PIbd-23_Ivanov_V.N._Pizzeria/Pizzeria/PizzeriaDatabaseImplement/Models/MessageInfo.cs

96 lines
2.7 KiB
C#
Raw Permalink Normal View History

2024-04-02 16:35:18 +04:00
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PizzeriaDatabaseImplement.Models
{
2024-04-03 09:19:16 +04:00
public class MessageInfo : IMessageInfoModel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string MessageId { get; set; } = string.Empty;
2024-04-02 16:35:18 +04:00
2024-04-03 09:19:16 +04:00
public int? ClientId { get; set; }
2024-04-02 16:35:18 +04:00
2024-04-03 09:19:16 +04:00
public virtual Client? Client { get; set; }
2024-04-02 16:35:18 +04:00
2024-04-03 09:19:16 +04:00
[Required]
public string SenderName { get; set; } = string.Empty;
2024-04-02 16:35:18 +04:00
2024-04-03 09:19:16 +04:00
[Required]
public DateTime DateDelivery { get; set; }
2024-04-02 16:35:18 +04:00
2024-04-03 09:19:16 +04:00
[Required]
public string Subject { get; set; } = string.Empty;
2024-04-02 16:35:18 +04:00
2024-04-03 09:19:16 +04:00
[Required]
public string Body { get; set; } = string.Empty;
2024-04-02 16:35:18 +04:00
2024-04-03 09:19:16 +04:00
[Required]
public bool IsReaded { get; set; }
2024-04-02 16:35:18 +04:00
2024-04-03 09:19:16 +04:00
public string? ReplyMessageId { get; set; }
2024-04-02 16:35:18 +04:00
2024-04-03 09:19:16 +04:00
[ForeignKey("ReplyMessageId")]
public virtual MessageInfo? Reply { get; set; }
[Required]
public bool IsReply { get; set; }
public static MessageInfo? Create(MessageInfoBindingModel? model)
{
if (model == null)
{
return null;
}
return new()
{
MessageId = model.MessageId,
ClientId = model.ClientId,
SenderName = model.SenderName,
DateDelivery = model.DateDelivery,
Subject = model.Subject,
Body = model.Body,
IsReaded = model.IsReaded,
ReplyMessageId = model.ReplyMessageId,
IsReply = model.IsReply
};
}
public void Update(PizzeriaDatabase context, MessageInfoBindingModel model)
{
if (model == null)
{
return;
}
IsReaded = model.IsReaded;
ReplyMessageId = model.ReplyMessageId;
if (!string.IsNullOrEmpty(ReplyMessageId))
{
Reply = context.MessageInfos.First(x => x.MessageId == ReplyMessageId);
}
}
public MessageInfoViewModel GetViewModel => new()
{
MessageId = MessageId,
ClientId = ClientId,
SenderName = SenderName,
DateDelivery = DateDelivery,
Subject = Subject,
Body = Body,
IsReaded = IsReaded,
ReplyMessageId = ReplyMessageId,
Reply = Reply,
IsReply = IsReply
};
}
2024-04-02 16:35:18 +04:00
}