PIbd-22-Ismailov_SUBD/BlogDataModels/BlogDatabaseImplement/Model/Message.cs

70 lines
1.8 KiB
C#
Raw Normal View History

2023-09-06 22:02:39 +04:00
using BlogContracts.BindingModels;
using BlogContracts.ViewModels;
using BlogDataModels;
2023-09-06 21:16:28 +04:00
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,
};
}
}