Files
Complex/Complex.Application/Services/AnnouncementService.cs

100 lines
3.1 KiB
C#

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; }
}
}