Component-oriented-programming/LibraryDatabase/Models/Author.cs

42 lines
1.1 KiB
C#

using LibraryDataModels.AbstractModels;
using LibraryDataModels.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibraryDatabase.Models
{
[Table("authors")]
public class Author : IAuthor
{
[Column("author_id")]
public int Id { get; private set; }
[Required]
[Column("author_name")]
public string Name { get; private set; } = string.Empty;
public Author() {}
public Author(IAuthor author)
{
Id = author.Id;
Name = author.Name;
}
public static Author Create(IAuthor author)
{
if (author == null)
throw new ArgumentNullException("Cannot create author because model is null");
return new Author(author);
}
public void Update(IAuthor author)
{
if (string.IsNullOrWhiteSpace(author?.Name))
throw new ArgumentNullException("Cannot update author because model is null");
Name = author.Name;
}
public AuthorView GetView => new AuthorView(this);
}
}