using ForumContracts.BindingModels; using ForumContracts.ViewModels; using ForumDataModels; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace BlogDatabase.Models { public class Message : IMessageModel { [Required] public string Text { get; set; } = string.Empty; [Required] public DateTime Date { get; set; } = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc); public int Id { get; private set; } [Required] public int UserId { get; set; } [Required] public int TopicId { get; set; } public virtual User? User { get; set; } public virtual Topic? Topic { get; set; } public static Message? Create(MessageBindingModel model) { if (model == null) { return null; } return new Message() { Id = model.Id, Text = model.Text, Date = model.Date, UserId = model.UserId, TopicId = model.TopicId, }; } public void Update(MessageBindingModel model) { if (model == null) { return; } Text = model.Text; Date = model.Date; } public MessageViewModel GetViewModel => new() { Id = Id, Text = Text, Date = Date, TopicId = TopicId, UserId = UserId, Username = User == null ? string.Empty : User.Username, TopicName = Topic == null ? string.Empty : Topic.Name, }; } }