From 0d6b4bee62b7e3d9202c321a56a144202f1898c4 Mon Sep 17 00:00:00 2001 From: "DESKTOP-2S5P7D1\\Windows 10" Date: Wed, 6 Sep 2023 14:52:20 +0700 Subject: [PATCH] api noti --- .../Controllers/NotifyController.cs | 112 ++++++++++++++++++ .../Requests/NotiRequest.cs | 15 +++ 2 files changed, 127 insertions(+) create mode 100644 BMA.EHR.Placement.Service/Controllers/NotifyController.cs create mode 100644 BMA.EHR.Placement.Service/Requests/NotiRequest.cs diff --git a/BMA.EHR.Placement.Service/Controllers/NotifyController.cs b/BMA.EHR.Placement.Service/Controllers/NotifyController.cs new file mode 100644 index 00000000..643efe88 --- /dev/null +++ b/BMA.EHR.Placement.Service/Controllers/NotifyController.cs @@ -0,0 +1,112 @@ +using BMA.EHR.Application.Repositories; +using BMA.EHR.Domain.Common; +using BMA.EHR.Domain.Extensions; +using BMA.EHR.Domain.Models.MetaData; +using BMA.EHR.Domain.Models.Notifications; +using BMA.EHR.Domain.Models.Placement; +using BMA.EHR.Domain.Shared; +using BMA.EHR.Infrastructure.Persistence; +using BMA.EHR.Placement.Service.Requests; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Swashbuckle.AspNetCore.Annotations; +using System.Security.Claims; +using System.Security.Cryptography; + +namespace BMA.EHR.Placement.Service.Controllers +{ + [Route("api/v{version:apiVersion}/placement/noti")] + [ApiVersion("1.0")] + [ApiController] + [Produces("application/json")] + [Authorize] + [SwaggerTag("ระบบบรรจุ")] + public class NotifyController : BaseController + { + private readonly PlacementRepository _repository; + private readonly ApplicationDBContext _context; + private readonly MinIOService _documentService; + private readonly IHttpContextAccessor _httpContextAccessor; + + public NotifyController(PlacementRepository repository, + ApplicationDBContext context, + MinIOService documentService, + IHttpContextAccessor httpContextAccessor) + { + _repository = repository; + _context = context; + _documentService = documentService; + _httpContextAccessor = httpContextAccessor; + } + + #region " Properties " + + private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; + + private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value; + + #endregion + + [HttpPost()] + public async Task> UpdatePropertyByUser([FromBody] NotiRequest req) + { + var profile = await _context.Profiles.FirstOrDefaultAsync(x => x.Id == req.ReceiverUserId); + if (profile == null) + return Error(GlobalMessages.DataNotFound); + + if (req.IsSendInbox == true) + { + _context.Inboxes.Add(new Inbox + { + Subject = req.Subject, + Body = req.Body, + ReceiverUserId = req.ReceiverUserId, + Payload = "", + CreatedUserId = FullName ?? "", + CreatedFullName = UserId ?? "System Administrator", + CreatedAt = DateTime.Now, + LastUpdateFullName = FullName ?? "System Administrator", + LastUpdateUserId = UserId ?? "", + LastUpdatedAt = DateTime.Now, + }); + } + if (req.IsSendInbox == true) + { + _context.Notifications.Add(new Notification + { + Body = req.Body, + ReceiverUserId = req.ReceiverUserId, + Type = "", + Payload = "", + CreatedUserId = FullName ?? "", + CreatedFullName = UserId ?? "System Administrator", + CreatedAt = DateTime.Now, + LastUpdateFullName = FullName ?? "System Administrator", + LastUpdateUserId = UserId ?? "", + LastUpdatedAt = DateTime.Now, + }); + } + if (req.IsSendMail == true) + { + // _context.Notifications.Add(new Notification + // { + // Body = req.Body, + // ReceiverUserId = req.ReceiverUserId, + // Type = "", + // Payload = "", + // CreatedUserId = FullName ?? "", + // CreatedFullName = UserId ?? "System Administrator", + // CreatedAt = DateTime.Now, + // LastUpdateFullName = FullName ?? "System Administrator", + // LastUpdateUserId = UserId ?? "", + // LastUpdatedAt = DateTime.Now, + // }); + } + await _context.SaveChangesAsync(); + + return Success(); + } + + } +} diff --git a/BMA.EHR.Placement.Service/Requests/NotiRequest.cs b/BMA.EHR.Placement.Service/Requests/NotiRequest.cs new file mode 100644 index 00000000..7ce5b4a6 --- /dev/null +++ b/BMA.EHR.Placement.Service/Requests/NotiRequest.cs @@ -0,0 +1,15 @@ +using BMA.EHR.Domain.Models.MetaData; +using Microsoft.EntityFrameworkCore; + +namespace BMA.EHR.Placement.Service.Requests +{ + public class NotiRequest + { + public string Subject { get; set; } + public string Body { get; set; } + public Guid ReceiverUserId { get; set; } + public bool IsSendMail { get; set; } + public bool IsSendInbox { get; set; } + public bool IsSendNotification { get; set; } + } +}