Skip to content

Commit e21a9bf

Browse files
add tag table and endpoints
1 parent ff349af commit e21a9bf

File tree

12 files changed

+487
-5
lines changed

12 files changed

+487
-5
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using taskmaster_api.Data.DTOs;
4+
using taskmaster_api.Services.Interface;
5+
6+
namespace taskmaster_api.Controllers
7+
{
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class TagController : ApplicationControllerBase
11+
{
12+
private readonly ITagService _tagService;
13+
14+
public TagController(ITagService tagService)
15+
{
16+
_tagService = tagService;
17+
}
18+
19+
[HttpGet]
20+
public IActionResult GetAllTags()
21+
{
22+
return ToHttpResult<List<TagDto>>(_tagService.GetAllTags());
23+
}
24+
25+
[HttpGet("{id}")]
26+
public IActionResult GetTag(int id)
27+
{
28+
return ToHttpResult<TagDto>(_tagService.GetTagById(id));
29+
}
30+
31+
[HttpPost]
32+
public IActionResult CreateTag(TagDto tagDto)
33+
{
34+
return ToHttpResult<TagDto>(_tagService.CreateTag(tagDto));
35+
}
36+
37+
[HttpPut("{id}")]
38+
public IActionResult UpdateTag(int id, TagDto tagDto)
39+
{
40+
return ToHttpResult<TagDto>(_tagService.UpdateTag(id, tagDto));
41+
}
42+
43+
[HttpDelete("{id}")]
44+
public IActionResult DeleteTag(int id)
45+
{
46+
return ToHttpResult(_tagService.DeleteTag(id));
47+
}
48+
}
49+
}

taskmaster-api/Data/Contexts/ApplicationDbContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public ApplicationDbContext(DbContextOptions options) : base(options)
1414
public virtual DbSet<TaskEntity> Tasks { get; set; }
1515
public virtual DbSet<CommentEntity> Comments { get; set; }
1616
public virtual DbSet<AttachmentEntity> Attachments { get; set; }
17+
public virtual DbSet<TagEntity> Tags { get; set; }
1718
}
1819
}

taskmaster-api/Data/DTOs/TagDto.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
namespace taskmaster_api.Data.DTOs
1+
using taskmaster_api.Data.DTOs.Interface;
2+
using taskmaster_api.Data.Entities;
3+
using taskmaster_api.Utilities;
4+
5+
namespace taskmaster_api.Data.DTOs
26
{
3-
public class TagDto
7+
public class TagDto : IDto<TagEntity>
48
{
9+
public int? Id { get; set; }
10+
public string Name { get; set; }
11+
12+
public TagDto()
13+
{
14+
}
15+
16+
public TagEntity ToEntity()
17+
{
18+
return EntityHelpers.ToEntity<TagDto, TagEntity>(this);
19+
}
520
}
621
}
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1-
namespace taskmaster_api.Data.Entities
1+
using Microsoft.AspNetCore.Identity;
2+
using System.ComponentModel.DataAnnotations.Schema;
3+
using System.ComponentModel.DataAnnotations;
4+
using taskmaster_api.Data.DTOs;
5+
using taskmaster_api.Utilities;
6+
using taskmaster_api.Data.Entities.Interface;
7+
8+
namespace taskmaster_api.Data.Entities
29
{
3-
public class TagEntity
10+
public class TagEntity : IEntity<TagDto>
411
{
12+
[Key]
13+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
14+
public int? Id { get; set; }
15+
16+
[Required]
17+
[StringLength(50)]
18+
public string Name { get; set; }
19+
20+
public TagDto ToDto()
21+
{
22+
return EntityHelpers.ToDto<TagEntity, TagDto>(this);
23+
}
524
}
625
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using taskmaster_api.Data.Entities;
2+
3+
namespace taskmaster_api.Data.Repositories.Interface
4+
{
5+
public interface ITagRepository
6+
{
7+
TagEntity GetTagById(int id);
8+
IEnumerable<TagEntity> GetAllTags();
9+
TagEntity CreateTag(TagEntity tag);
10+
TagEntity UpdateTag(int id, TagEntity tag);
11+
int DeleteTag(int id);
12+
}
13+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using taskmaster_api.Data.Contexts;
3+
using taskmaster_api.Data.Entities;
4+
using taskmaster_api.Data.Repositories.Interface;
5+
6+
namespace taskmaster_api.Data.Repositories
7+
{
8+
public class TagRepository : ITagRepository
9+
{
10+
private readonly ApplicationDbContext _context;
11+
12+
public TagRepository(ApplicationDbContext context)
13+
{
14+
_context = context;
15+
}
16+
17+
public TagEntity GetTagById(int id)
18+
{
19+
return _context.Tags.Find(id);
20+
}
21+
22+
public IEnumerable<TagEntity> GetAllTags()
23+
{
24+
return _context.Tags.ToList();
25+
}
26+
27+
public TagEntity CreateTag(TagEntity tag)
28+
{
29+
_context.Tags.Add(tag);
30+
_context.SaveChanges();
31+
return tag;
32+
}
33+
34+
public TagEntity UpdateTag(int id, TagEntity tag)
35+
{
36+
if (_context.Tags.Find(id) is TagEntity oldTag)
37+
{
38+
tag.Id = id;
39+
_context.Tags.Entry(oldTag).State = EntityState.Detached;
40+
_context.Tags.Entry(tag).State = EntityState.Modified;
41+
_context.SaveChanges();
42+
}
43+
return tag;
44+
}
45+
46+
public int DeleteTag(int id)
47+
{
48+
var tagToDelete = _context.Tags.Find(id);
49+
if (tagToDelete != null)
50+
{
51+
_context.Tags.Remove(tagToDelete);
52+
_context.SaveChanges();
53+
return id;
54+
}
55+
return -1;
56+
}
57+
}
58+
}

taskmaster-api/Migrations/20231211062434_InitialCreate.Designer.cs renamed to taskmaster-api/Migrations/20231211082219_InitialCreate.Designer.cs

Lines changed: 73 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

taskmaster-api/Migrations/20231211062434_InitialCreate.cs renamed to taskmaster-api/Migrations/20231211082219_InitialCreate.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ protected override void Up(MigrationBuilder migrationBuilder)
5050
table.PrimaryKey("PK_AspNetUsers", x => x.Id);
5151
});
5252

53+
migrationBuilder.CreateTable(
54+
name: "Tags",
55+
columns: table => new
56+
{
57+
Id = table.Column<int>(type: "int", nullable: false)
58+
.Annotation("SqlServer:Identity", "1, 1"),
59+
Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false)
60+
},
61+
constraints: table =>
62+
{
63+
table.PrimaryKey("PK_Tags", x => x.Id);
64+
});
65+
5366
migrationBuilder.CreateTable(
5467
name: "Tasks",
5568
columns: table => new
@@ -174,6 +187,35 @@ protected override void Up(MigrationBuilder migrationBuilder)
174187
onDelete: ReferentialAction.Cascade);
175188
});
176189

190+
migrationBuilder.CreateTable(
191+
name: "Attachments",
192+
columns: table => new
193+
{
194+
Id = table.Column<int>(type: "int", nullable: false)
195+
.Annotation("SqlServer:Identity", "1, 1"),
196+
UserId = table.Column<string>(type: "nvarchar(450)", nullable: false),
197+
TaskId = table.Column<int>(type: "int", nullable: false),
198+
FileName = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
199+
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
200+
UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: false)
201+
},
202+
constraints: table =>
203+
{
204+
table.PrimaryKey("PK_Attachments", x => x.Id);
205+
table.ForeignKey(
206+
name: "FK_Attachments_AspNetUsers_UserId",
207+
column: x => x.UserId,
208+
principalTable: "AspNetUsers",
209+
principalColumn: "Id",
210+
onDelete: ReferentialAction.Cascade);
211+
table.ForeignKey(
212+
name: "FK_Attachments_Tasks_TaskId",
213+
column: x => x.TaskId,
214+
principalTable: "Tasks",
215+
principalColumn: "Id",
216+
onDelete: ReferentialAction.Cascade);
217+
});
218+
177219
migrationBuilder.CreateTable(
178220
name: "Comments",
179221
columns: table => new
@@ -242,6 +284,16 @@ protected override void Up(MigrationBuilder migrationBuilder)
242284
unique: true,
243285
filter: "[NormalizedUserName] IS NOT NULL");
244286

287+
migrationBuilder.CreateIndex(
288+
name: "IX_Attachments_TaskId",
289+
table: "Attachments",
290+
column: "TaskId");
291+
292+
migrationBuilder.CreateIndex(
293+
name: "IX_Attachments_UserId",
294+
table: "Attachments",
295+
column: "UserId");
296+
245297
migrationBuilder.CreateIndex(
246298
name: "IX_Comments_TaskId",
247299
table: "Comments",
@@ -271,9 +323,15 @@ protected override void Down(MigrationBuilder migrationBuilder)
271323
migrationBuilder.DropTable(
272324
name: "AspNetUserTokens");
273325

326+
migrationBuilder.DropTable(
327+
name: "Attachments");
328+
274329
migrationBuilder.DropTable(
275330
name: "Comments");
276331

332+
migrationBuilder.DropTable(
333+
name: "Tags");
334+
277335
migrationBuilder.DropTable(
278336
name: "AspNetRoles");
279337

0 commit comments

Comments
 (0)