Skip to content

Commit c238042

Browse files
- add attachment table and endpoints
- implment file upload logic
1 parent 575e4f5 commit c238042

File tree

12 files changed

+373
-5
lines changed

12 files changed

+373
-5
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,7 @@ MigrationBackup/
360360
.ionide/
361361

362362
# Fody - auto-generated XML schema
363-
FodyWeavers.xsd
363+
FodyWeavers.xsd
364+
365+
# Upload foler
366+
taskmaster-api/Uploads/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 AttachmentController : ApplicationControllerBase
11+
{
12+
private readonly IAttachmentService _attachmentService;
13+
14+
public AttachmentController(IAttachmentService attachmentService)
15+
{
16+
_attachmentService = attachmentService;
17+
}
18+
19+
[HttpGet]
20+
public IActionResult GetAllAttachments()
21+
{
22+
return ToHttpResult<List<AttachmentDto>>(_attachmentService.GetAllAttachments());
23+
}
24+
25+
[HttpGet("{id}")]
26+
public IActionResult GetAttachment(int id)
27+
{
28+
return ToHttpResult<AttachmentDto>(_attachmentService.GetAttachmentById(id));
29+
}
30+
31+
[HttpPost]
32+
public IActionResult CreateAttachment(AttachmentDto attachmentDto)
33+
{
34+
return ToHttpResult<AttachmentDto>(_attachmentService.CreateAttachment(attachmentDto));
35+
}
36+
37+
[HttpPut("{id}")]
38+
public IActionResult UpdateAttachment(int id, AttachmentDto attachmentDto)
39+
{
40+
return ToHttpResult<AttachmentDto>(_attachmentService.UpdateAttachment(id, attachmentDto));
41+
}
42+
43+
[HttpDelete("{id}")]
44+
public IActionResult DeleteAttachment(int id)
45+
{
46+
return ToHttpResult(_attachmentService.DeleteAttachment(id));
47+
}
48+
49+
[HttpPost("upload")]
50+
public IActionResult Upload([FromForm] AttachmentUploadRequest request)
51+
{
52+
return ToHttpResult(_attachmentService.UploadFile(request));
53+
}
54+
}
55+
}

taskmaster-api/Data/Contexts/ApplicationDbContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public ApplicationDbContext(DbContextOptions options) : base(options)
1313

1414
public virtual DbSet<TaskEntity> Tasks { get; set; }
1515
public virtual DbSet<CommentEntity> Comments { get; set; }
16+
public virtual DbSet<AttachmentEntity> Attachments { get; set; }
1617
}
1718
}
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
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 AttachmentDto
7+
public class AttachmentDto : IDto<AttachmentEntity>
48
{
9+
public int? Id { get; set; }
10+
public string UserId { get; set; }
11+
public int TaskId { get; set; }
12+
public string FileName { get; set; }
13+
public DateTime CreatedAt { get; set; }
14+
public DateTime UpdatedAt { get; set; }
15+
16+
public AttachmentDto()
17+
{
18+
if (Id.HasValue)
19+
{
20+
UpdatedAt = DateTime.UtcNow;
21+
}
22+
else
23+
{
24+
CreatedAt = DateTime.UtcNow;
25+
UpdatedAt = DateTime.UtcNow;
26+
}
27+
}
28+
29+
public AttachmentEntity ToEntity()
30+
{
31+
return EntityHelpers.ToEntity<AttachmentDto, AttachmentEntity>(this);
32+
}
533
}
634
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace taskmaster_api.Data.DTOs
2+
{
3+
public class AttachmentUploadRequest
4+
{
5+
public IFormFile File { get; set; }
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace taskmaster_api.Data.DTOs
2+
{
3+
public class AttachmentUploadResult
4+
{
5+
public bool Success { get; set; }
6+
public string FilePath { get; set; }
7+
public string FileName { get; set; }
8+
}
9+
}
Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
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.Data.Entities.Interface;
6+
using taskmaster_api.Utilities;
7+
8+
namespace taskmaster_api.Data.Entities
29
{
3-
public class AttachmentEntity
10+
public class AttachmentEntity : IEntity<AttachmentDto>
411
{
12+
[Key]
13+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
14+
public int? Id { get; set; }
15+
16+
[Required]
17+
public string UserId { get; set; }
18+
public IdentityUser User { get; set; }
19+
20+
[Required]
21+
public int TaskId { get; set; }
22+
public TaskEntity Task { get; set; }
23+
24+
[Required]
25+
[StringLength(255)]
26+
public string FileName { get; set; }
27+
28+
public DateTime CreatedAt { get; set; }
29+
30+
public DateTime UpdatedAt { get; set; }
31+
32+
public AttachmentDto ToDto()
33+
{
34+
return EntityHelpers.ToDto<AttachmentEntity, AttachmentDto>(this);
35+
}
536
}
637
}
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 AttachmentRepository : IAttachmentRepository
9+
{
10+
private readonly ApplicationDbContext _context;
11+
12+
public AttachmentRepository(ApplicationDbContext context)
13+
{
14+
_context = context;
15+
}
16+
17+
public AttachmentEntity GetAttachmentById(int id)
18+
{
19+
return _context.Attachments.Find(id);
20+
}
21+
22+
public IEnumerable<AttachmentEntity> GetAllAttachments()
23+
{
24+
return _context.Attachments.ToList();
25+
}
26+
27+
public AttachmentEntity CreateAttachment(AttachmentEntity attachment)
28+
{
29+
_context.Attachments.Add(attachment);
30+
_context.SaveChanges();
31+
return attachment;
32+
}
33+
34+
public AttachmentEntity UpdateAttachment(int id, AttachmentEntity attachment)
35+
{
36+
if (_context.Attachments.Find(id) is AttachmentEntity oldAttachment)
37+
{
38+
attachment.Id = id;
39+
_context.Attachments.Entry(oldAttachment).State = EntityState.Detached;
40+
_context.Attachments.Entry(attachment).State = EntityState.Modified;
41+
_context.SaveChanges();
42+
}
43+
return attachment;
44+
}
45+
46+
public int DeleteAttachment(int id)
47+
{
48+
var attachmentToDelete = _context.Attachments.Find(id);
49+
if (attachmentToDelete != null)
50+
{
51+
_context.Attachments.Remove(attachmentToDelete);
52+
_context.SaveChanges();
53+
return id;
54+
}
55+
return -1;
56+
}
57+
}
58+
}
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 IAttachmentRepository
6+
{
7+
AttachmentEntity GetAttachmentById(int id);
8+
IEnumerable<AttachmentEntity> GetAllAttachments();
9+
AttachmentEntity CreateAttachment(AttachmentEntity attachment);
10+
AttachmentEntity UpdateAttachment(int id, AttachmentEntity attachment);
11+
int DeleteAttachment(int id);
12+
}
13+
}

taskmaster-api/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@
6161

6262
builder.Services.AddScoped<ITaskRepository, TaskRepository>();
6363
builder.Services.AddScoped<ICommentRepository, CommentRepository>();
64+
builder.Services.AddScoped<IAttachmentRepository, AttachmentRepository>();
65+
6466
builder.Services.AddScoped<ITaskService, TaskService>();
6567
builder.Services.AddScoped<IAuthService, AuthService>();
6668
builder.Services.AddScoped<IUserService, UserService>();
6769
builder.Services.AddScoped<ICommentService, CommentService>();
70+
builder.Services.AddScoped<IAttachmentService, AttachmentService>();
6871

6972
var app = builder.Build();
7073

0 commit comments

Comments
 (0)