88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
using ForumContracts.BindingModels;
|
|
using ForumContracts.ViewModels;
|
|
using ForumDataModels.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ForumDatabaseImplement.Models
|
|
{
|
|
public class Question : IQuestionModel
|
|
{
|
|
[Required]
|
|
public DateTime CreateDate { get; set; } = DateTime.Now;
|
|
|
|
public int UserId { get; set; }
|
|
|
|
public int CategoryId { get; set; }
|
|
|
|
public int Id { get; set; }
|
|
|
|
[Required]
|
|
public string QuestionDes { get; set; } = string.Empty;
|
|
|
|
public virtual User User { get; set; }
|
|
|
|
public virtual Category Category { get; set; }
|
|
|
|
[ForeignKey("QuestionId")]
|
|
public virtual List<Answer> Answers { get; set; } = new();
|
|
|
|
public static Question? Create(QuestionBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Question()
|
|
{
|
|
Id = model.Id,
|
|
CreateDate = model.CreateDate,
|
|
UserId = model.UserId,
|
|
CategoryId = model.CategoryId,
|
|
QuestionDes = model.QuestionDes
|
|
};
|
|
}
|
|
public static Question Create(QuestionViewModel model)
|
|
{
|
|
return new Question
|
|
{
|
|
Id = model.Id,
|
|
UserId = model.UserId,
|
|
CategoryId = model.CategoryId,
|
|
CreateDate = model.CreateDate,
|
|
QuestionDes = model.QuestionDes
|
|
};
|
|
}
|
|
public void Update(QuestionBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
UserId = model.UserId;
|
|
CategoryId = model.CategoryId;
|
|
CreateDate = model.CreateDate;
|
|
QuestionDes = model.QuestionDes;
|
|
}
|
|
public QuestionViewModel GetViewModel()
|
|
{
|
|
return new()
|
|
{
|
|
Id = Id,
|
|
UserId = UserId,
|
|
UserName = User?.Nickname ?? string.Empty,
|
|
CategoryId = CategoryId,
|
|
CreateDate = CreateDate,
|
|
QuestionDes = QuestionDes,
|
|
CategoryName = Category?.Name
|
|
};
|
|
}
|
|
}
|
|
}
|