Skip to content

Commit 0f601ce

Browse files
add tables and endpoints for notification and setting
1 parent be1880c commit 0f601ce

19 files changed

+776
-11
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Mvc;
4+
using taskmaster_api.Data.DTOs;
5+
using taskmaster_api.Services.Interface;
6+
7+
namespace taskmaster_api.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
[Authorize]
12+
public class NotificationController : ApplicationControllerBase
13+
{
14+
private readonly INotificationService _notificationService;
15+
16+
public NotificationController(INotificationService notificationService)
17+
{
18+
_notificationService = notificationService;
19+
}
20+
21+
[HttpGet]
22+
public IActionResult GetAllNotifications()
23+
{
24+
return ToHttpResult<List<NotificationDto>>(_notificationService.GetAllNotifications());
25+
}
26+
27+
[HttpGet("{id}")]
28+
public IActionResult GetNotification(int id)
29+
{
30+
return ToHttpResult<NotificationDto>(_notificationService.GetNotificationById(id));
31+
}
32+
33+
[HttpPost]
34+
public IActionResult CreateNotification(NotificationDto notificationDto)
35+
{
36+
return ToHttpResult<NotificationDto>(_notificationService.CreateNotification(notificationDto));
37+
}
38+
39+
[HttpPut("{id}")]
40+
public IActionResult UpdateNotification(int id, NotificationDto notificationDto)
41+
{
42+
return ToHttpResult<NotificationDto>(_notificationService.UpdateNotification(id, notificationDto));
43+
}
44+
45+
[HttpDelete("{id}")]
46+
public IActionResult DeleteNotification(int id)
47+
{
48+
return ToHttpResult(_notificationService.DeleteNotification(id));
49+
}
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Mvc;
4+
using taskmaster_api.Data.DTOs;
5+
using taskmaster_api.Services.Interface;
6+
7+
namespace taskmaster_api.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
[Authorize]
12+
public class SettingController : ApplicationControllerBase
13+
{
14+
private readonly ISettingService _settingService;
15+
16+
public SettingController(ISettingService settingService)
17+
{
18+
_settingService = settingService;
19+
}
20+
21+
[HttpGet]
22+
public IActionResult GetAllSettings()
23+
{
24+
return ToHttpResult<List<SettingDto>>(_settingService.GetAllSettings());
25+
}
26+
27+
[HttpGet("{id}")]
28+
public IActionResult GetSetting(int id)
29+
{
30+
return ToHttpResult<SettingDto>(_settingService.GetSettingById(id));
31+
}
32+
33+
[HttpPost]
34+
public IActionResult CreateSetting(SettingDto settingDto)
35+
{
36+
return ToHttpResult<SettingDto>(_settingService.CreateSetting(settingDto));
37+
}
38+
39+
[HttpPut("{id}")]
40+
public IActionResult UpdateSetting(int id, SettingDto settingDto)
41+
{
42+
return ToHttpResult<SettingDto>(_settingService.UpdateSetting(id, settingDto));
43+
}
44+
45+
[HttpDelete("{id}")]
46+
public IActionResult DeleteSetting(int id)
47+
{
48+
return ToHttpResult(_settingService.DeleteSetting(id));
49+
}
50+
}
51+
}

taskmaster-api/Data/Contexts/ApplicationDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ public ApplicationDbContext(DbContextOptions options) : base(options)
1616
public virtual DbSet<AttachmentEntity> Attachments { get; set; }
1717
public virtual DbSet<TagEntity> Tags { get; set; }
1818
public virtual DbSet<ActivityLogEntity> ActivityLogs { get; set; }
19+
public virtual DbSet<NotificationEntity> Notifications { get; set; }
20+
public virtual DbSet<SettingEntity> Settings { get; set; }
1921
}
2022
}
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
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 NotificationDto
7+
public class NotificationDto : IDto<NotificationEntity>
48
{
9+
public int? Id { get; set; }
10+
public string UserId { get; set; }
11+
public string Content { get; set; }
12+
public bool IsRead { get; set; }
13+
public DateTime CreatedAt { get; set; }
14+
15+
public NotificationDto()
16+
{
17+
if (!Id.HasValue)
18+
{
19+
IsRead = false;
20+
CreatedAt = DateTime.UtcNow;
21+
}
22+
}
23+
24+
public NotificationEntity ToEntity()
25+
{
26+
return EntityHelpers.ToEntity<NotificationDto, NotificationEntity>(this);
27+
}
528
}
629
}
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
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 SettingDto
7+
public class SettingDto : IDto<SettingEntity>
48
{
9+
public int? Id { get; set; }
10+
public string Name { get; set; }
11+
public string Value { get; set; }
12+
public string? Description { get; set; }
13+
14+
public SettingDto()
15+
{
16+
}
17+
18+
public SettingEntity ToEntity()
19+
{
20+
return EntityHelpers.ToEntity<SettingDto, SettingEntity>(this);
21+
}
522
}
623
}
Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
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 NotificationEntity
10+
public class NotificationEntity : IEntity<NotificationDto>
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 string Content { get; set; }
22+
23+
[Required]
24+
public bool IsRead { get; set; }
25+
26+
public DateTime CreatedAt { get; set; }
27+
28+
public NotificationDto ToDto()
29+
{
30+
return EntityHelpers.ToDto<NotificationEntity, NotificationDto>(this);
31+
}
532
}
633
}
Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
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 SettingEntity
10+
public class SettingEntity : IEntity<SettingDto>
411
{
12+
[Key]
13+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
14+
public int? Id { get; set; }
15+
16+
[Required]
17+
[StringLength(100)]
18+
public string Name { get; set; }
19+
20+
[Required]
21+
public string Value { get; set; }
22+
23+
public string? Description { get; set; }
24+
25+
public SettingDto ToDto()
26+
{
27+
return EntityHelpers.ToDto<SettingEntity, SettingDto>(this);
28+
}
529
}
630
}
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 INotificationRepository
6+
{
7+
NotificationEntity GetNotificationById(int id);
8+
IEnumerable<NotificationEntity> GetAllNotifications();
9+
NotificationEntity CreateNotification(NotificationEntity notification);
10+
NotificationEntity UpdateNotification(int id, NotificationEntity notification);
11+
int DeleteNotification(int id);
12+
}
13+
}
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 ISettingRepository
6+
{
7+
SettingEntity GetSettingById(int id);
8+
IEnumerable<SettingEntity> GetAllSettings();
9+
SettingEntity CreateSetting(SettingEntity setting);
10+
SettingEntity UpdateSetting(int id, SettingEntity setting);
11+
int DeleteSetting(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 NotificationRepository : INotificationRepository
9+
{
10+
private readonly ApplicationDbContext _context;
11+
12+
public NotificationRepository(ApplicationDbContext context)
13+
{
14+
_context = context;
15+
}
16+
17+
public NotificationEntity GetNotificationById(int id)
18+
{
19+
return _context.Notifications.Find(id);
20+
}
21+
22+
public IEnumerable<NotificationEntity> GetAllNotifications()
23+
{
24+
return _context.Notifications.ToList();
25+
}
26+
27+
public NotificationEntity CreateNotification(NotificationEntity notification)
28+
{
29+
_context.Notifications.Add(notification);
30+
_context.SaveChanges();
31+
return notification;
32+
}
33+
34+
public NotificationEntity UpdateNotification(int id, NotificationEntity notification)
35+
{
36+
if (_context.Notifications.Find(id) is NotificationEntity oldNotification)
37+
{
38+
notification.Id = id;
39+
_context.Notifications.Entry(oldNotification).State = EntityState.Detached;
40+
_context.Notifications.Entry(notification).State = EntityState.Modified;
41+
_context.SaveChanges();
42+
}
43+
return notification;
44+
}
45+
46+
public int DeleteNotification(int id)
47+
{
48+
var notificationToDelete = _context.Notifications.Find(id);
49+
if (notificationToDelete != null)
50+
{
51+
_context.Notifications.Remove(notificationToDelete);
52+
_context.SaveChanges();
53+
return id;
54+
}
55+
return -1;
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)