From aacbcfee32e594cfb9ac9b193006ec161cd80c96 Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Wed, 9 Aug 2023 20:28:56 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B8=9A=E0=B8=B1?= =?UTF-8?q?=E0=B9=89=E0=B8=81=20=E0=B9=81=E0=B8=A5=E0=B8=B0=E0=B9=80?= =?UTF-8?q?=E0=B8=9E=E0=B8=B4=E0=B9=88=E0=B8=A1=20api=20inbox=20noti?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApplicationServicesRegistration.cs | 2 + .../Commands/CommandRepository.cs | 12 ++- .../MessageQueue/InboxRepository.cs | 59 ++++++++++++ .../MessageQueue/NotificationRepository.cs | 59 ++++++++++++ .../Controllers/MessageController.cs | 90 +++++++++++++++++++ .../Controllers/OrderController.cs | 3 +- 6 files changed, 220 insertions(+), 5 deletions(-) create mode 100644 BMA.EHR.Application/Repositories/MessageQueue/InboxRepository.cs create mode 100644 BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs create mode 100644 BMA.EHR.Command.Service/Controllers/MessageController.cs diff --git a/BMA.EHR.Application/ApplicationServicesRegistration.cs b/BMA.EHR.Application/ApplicationServicesRegistration.cs index 16b36ace..bac9063a 100644 --- a/BMA.EHR.Application/ApplicationServicesRegistration.cs +++ b/BMA.EHR.Application/ApplicationServicesRegistration.cs @@ -20,6 +20,8 @@ namespace BMA.EHR.Application services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); return services; } diff --git a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs index 723106b7..6b7879b0 100644 --- a/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs +++ b/BMA.EHR.Application/Repositories/Commands/CommandRepository.cs @@ -1007,15 +1007,21 @@ namespace BMA.EHR.Application.Repositories.Commands } } - public async Task UpdatePlacementSalaryAsync(Guid placementProfileId, UpdatePlacementSalaryRequest req) + public async Task UpdatePlacementSalaryAsync(Guid personalId, UpdatePlacementSalaryRequest req) { try { + var current = await _dbContext.Set() + .FirstOrDefaultAsync(x => x.Id == personalId); + + if (current == null) + throw new Exception(GlobalMessages.DataNotFound); + var placementProfile = await _dbContext.Set() - .FirstOrDefaultAsync(p => p.Id == placementProfileId); + .FirstOrDefaultAsync(p => p.Id == current.RefPlacementProfileId); if (placementProfile == null) - throw new Exception($"Invalid placement profile: {placementProfileId}"); + throw new Exception($"Invalid placement profile: {current.RefPlacementProfileId}"); placementProfile.Amount = req.SalaryAmount; placementProfile.PositionSalaryAmount = req.PositionSalaryAmount; diff --git a/BMA.EHR.Application/Repositories/MessageQueue/InboxRepository.cs b/BMA.EHR.Application/Repositories/MessageQueue/InboxRepository.cs new file mode 100644 index 00000000..917ea815 --- /dev/null +++ b/BMA.EHR.Application/Repositories/MessageQueue/InboxRepository.cs @@ -0,0 +1,59 @@ +using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Domain.Models.HR; +using BMA.EHR.Domain.Models.Notifications; +using BMA.EHR.Domain.Shared; +using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; + +namespace BMA.EHR.Application.Repositories.MessageQueue +{ + public class InboxRepository : GenericRepository + { + #region " Fields " + + private readonly IApplicationDBContext _dbContext; + private readonly IHttpContextAccessor _httpContextAccessor; + + #endregion + + #region " Constructor and Destuctor " + + public InboxRepository(IApplicationDBContext dbContext, + IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor) + { + _dbContext = dbContext; + _httpContextAccessor = httpContextAccessor; + } + + #endregion + + #region " Methods " + + public async Task> GetMyInboxAsync() + { + try + { + var profile = await _dbContext.Set() + .FirstOrDefaultAsync(p => p.KeycloakId == Guid.Parse(UserId!)); + + if (profile == null) + { + throw new Exception(GlobalMessages.DataNotFound); + } + + var data = await _dbContext.Set() + .Where(x => x.ReceiverUserId == profile.Id) + .OrderByDescending(x => x.ReceiveDate) + .ToListAsync(); + + return data; + } + catch + { + throw; + } + } + + #endregion + } +} diff --git a/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs b/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs new file mode 100644 index 00000000..11f7c0ca --- /dev/null +++ b/BMA.EHR.Application/Repositories/MessageQueue/NotificationRepository.cs @@ -0,0 +1,59 @@ +using BMA.EHR.Application.Common.Interfaces; +using BMA.EHR.Domain.Models.HR; +using BMA.EHR.Domain.Models.Notifications; +using BMA.EHR.Domain.Shared; +using Microsoft.AspNetCore.Http; +using Microsoft.EntityFrameworkCore; + +namespace BMA.EHR.Application.Repositories.MessageQueue +{ + public class NotificationRepository : GenericRepository + { + #region " Fields " + + private readonly IApplicationDBContext _dbContext; + private readonly IHttpContextAccessor _httpContextAccessor; + + #endregion + + #region " Constructor and Destuctor " + + public NotificationRepository(IApplicationDBContext dbContext, + IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor) + { + _dbContext = dbContext; + _httpContextAccessor = httpContextAccessor; + } + + #endregion + + #region " Methods " + + public async Task> GetMyNotificationAsync() + { + try + { + var profile = await _dbContext.Set() + .FirstOrDefaultAsync(p => p.KeycloakId == Guid.Parse(UserId!)); + + if (profile == null) + { + throw new Exception(GlobalMessages.DataNotFound); + } + + var data = await _dbContext.Set() + .Where(x => x.ReceiverUserId == profile.Id) + .OrderByDescending(x => x.ReceiveDate) + .ToListAsync(); + + return data; + } + catch + { + throw; + } + } + + #endregion + } +} diff --git a/BMA.EHR.Command.Service/Controllers/MessageController.cs b/BMA.EHR.Command.Service/Controllers/MessageController.cs new file mode 100644 index 00000000..96282067 --- /dev/null +++ b/BMA.EHR.Command.Service/Controllers/MessageController.cs @@ -0,0 +1,90 @@ +using BMA.EHR.Application.Repositories.MessageQueue; +using BMA.EHR.Domain.Common; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Swashbuckle.AspNetCore.Annotations; + +namespace BMA.EHR.Command.Service.Controllers +{ + [Route("api/v{version:apiVersion}/message")] + [ApiVersion("1.0")] + [ApiController] + [Produces("application/json")] + [Authorize] + [SwaggerTag("API ระบบ Inbox และ Notification")] + public class MessageController : BaseController + { + #region " Fields " + + private readonly InboxRepository _inboxRepository; + private readonly NotificationRepository _notificationRepository; + + #endregion + + #region " Constuctor and Destructor " + + public MessageController(InboxRepository inboxRepository, + NotificationRepository notificationRepository) + { + _inboxRepository = inboxRepository; + _notificationRepository = notificationRepository; + } + + #endregion + + #region " Methods " + + /// + /// แสดงข้อมูล Inbox ของ user ที่ Login + /// + /// + /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("my-inboxes")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetMyInboxAsync() + { + try + { + var inboxes = await _inboxRepository.GetMyInboxAsync(); + + return Success(inboxes); + } + catch + { + throw; + } + } + + /// + /// แสดงข้อมูล Notification ของ user ที่ Login + /// + /// + /// เมื่อทำการอ่านข้อมูลจาก Relational Database สำเร็จ + /// ไม่ได้ Login เข้าระบบ + /// เมื่อเกิดข้อผิดพลาดในการทำงาน + [HttpGet("my-notifications")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task> GetMyNotificationAsync() + { + try + { + var noti = await _notificationRepository.GetMyNotificationAsync(); + + return Success(noti); + } + catch + { + throw; + } + } + + + #endregion + } +} diff --git a/BMA.EHR.Command.Service/Controllers/OrderController.cs b/BMA.EHR.Command.Service/Controllers/OrderController.cs index 1f37cc50..b6b4f81e 100644 --- a/BMA.EHR.Command.Service/Controllers/OrderController.cs +++ b/BMA.EHR.Command.Service/Controllers/OrderController.cs @@ -1,5 +1,4 @@ -using Amazon.S3.Model.Internal.MarshallTransformations; -using BMA.EHR.Application.Repositories; +using BMA.EHR.Application.Repositories; using BMA.EHR.Application.Repositories.Commands; using BMA.EHR.Application.Requests.Commands; using BMA.EHR.Command.Service.Requests;