feat: add Announcement service with GetAnnouncements and SendAnnouncement
This commit is contained in:
99
Complex.Application/Services/AnnouncementService.cs
Normal file
99
Complex.Application/Services/AnnouncementService.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using Complex.Application;
|
||||
using Complex.Common.Dto;
|
||||
using Complex.Domain.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Complex.Application.Services.Announcement
|
||||
{
|
||||
public interface IAnnouncementService
|
||||
{
|
||||
ResultDto<List<AnnouncementDto>> GetAnnouncements(Guid complexId, int page = 1, int pageSize = 20);
|
||||
ResultDto<long> SendAnnouncement(Guid complexId, string body, byte? chatTypeId, string userPhone);
|
||||
}
|
||||
|
||||
public class AnnouncementService : IAnnouncementService
|
||||
{
|
||||
private readonly IComplexDBContext _complexDBContext;
|
||||
|
||||
public AnnouncementService(IComplexDBContext complexDBContext)
|
||||
{
|
||||
_complexDBContext = complexDBContext;
|
||||
}
|
||||
|
||||
public ResultDto<List<AnnouncementDto>> GetAnnouncements(Guid complexId, int page = 1, int pageSize = 20)
|
||||
{
|
||||
var query = _complexDBContext.Chats
|
||||
.Where(c => c.ComplexId == complexId)
|
||||
.OrderByDescending(c => c.Id);
|
||||
|
||||
var totalCount = query.Count();
|
||||
|
||||
var chats = query
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToList();
|
||||
|
||||
var result = chats.Select(c => new AnnouncementDto
|
||||
{
|
||||
Id = c.Id,
|
||||
ComplexId = c.ComplexId,
|
||||
PersonMobileNumber = c.PersonMobileNumber,
|
||||
Body = c.Body,
|
||||
ChatTypeId = c.ChatTypeId,
|
||||
InsertTime = null // InsertTime is set by Auditable attribute in SaveChanges, not a property on Chat entity
|
||||
}).ToList();
|
||||
|
||||
return new ResultDto<List<AnnouncementDto>>
|
||||
{
|
||||
Data = result,
|
||||
IsSuccess = true,
|
||||
RowCount = totalCount
|
||||
};
|
||||
}
|
||||
|
||||
public ResultDto<long> SendAnnouncement(Guid complexId, string body, byte? chatTypeId, string userPhone)
|
||||
{
|
||||
var chat = new Chat
|
||||
{
|
||||
ComplexId = complexId,
|
||||
Body = body,
|
||||
ChatTypeId = chatTypeId ?? 0,
|
||||
PersonMobileNumber = userPhone
|
||||
};
|
||||
|
||||
_complexDBContext.Chats.Add(chat);
|
||||
_complexDBContext.SaveChanges();
|
||||
|
||||
return new ResultDto<long>
|
||||
{
|
||||
Data = chat.Id,
|
||||
IsSuccess = true,
|
||||
Message = "ثبت با موفقیت انجام شد"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class AnnouncementDto
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public Guid ComplexId { get; set; }
|
||||
public string? PersonMobileNumber { get; set; }
|
||||
public string? Body { get; set; }
|
||||
public byte? ChatTypeId { get; set; }
|
||||
public string? InsertTime { get; set; }
|
||||
}
|
||||
|
||||
public class AnnouncementListRequest
|
||||
{
|
||||
public Guid ComplexId { get; set; }
|
||||
public int Page { get; set; } = 1;
|
||||
public int PageSize { get; set; } = 20;
|
||||
}
|
||||
|
||||
public class SendAnnouncementRequest
|
||||
{
|
||||
public Guid ComplexId { get; set; }
|
||||
public string Body { get; set; }
|
||||
public byte? ChatTypeId { get; set; }
|
||||
}
|
||||
}
|
||||
34
Complex.EndPoint/Controllers/AnnouncementController.cs
Normal file
34
Complex.EndPoint/Controllers/AnnouncementController.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Complex.Application.Services.Announcement;
|
||||
using Complex.Common.Dto;
|
||||
using Complex.EndPoint.Utility;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Complex.EndPoint.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[ApiVersion("1")]
|
||||
[Route("api/v{version:apiVersion}/[controller]/[action]")]
|
||||
[Authorize]
|
||||
public class AnnouncementController : Controller
|
||||
{
|
||||
private readonly IAnnouncementService _announcementService;
|
||||
|
||||
public AnnouncementController(IAnnouncementService announcementService)
|
||||
{
|
||||
_announcementService = announcementService;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ResultDto<List<AnnouncementDto>> GetAnnouncements(AnnouncementListRequest request)
|
||||
{
|
||||
return _announcementService.GetAnnouncements(request.ComplexId, request.Page, request.PageSize);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ResultDto<long> SendAnnouncement(SendAnnouncementRequest request)
|
||||
{
|
||||
return _announcementService.SendAnnouncement(request.ComplexId, request.Body, request.ChatTypeId, User.GetUserId());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user