PIAPS_CW/DatabaseImplement/Models/MediaFile.cs

62 lines
1.5 KiB
C#

using Contracts.BindingModels;
using Contracts.ViewModels;
using DataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement.Models
{
public class MediaFile : IMediaFile
{
[Required]
public Guid Id { get; set; }
[Required]
public Guid ProductId { get; set; }
[Required]
public byte[] Image { get; set; }
public virtual Product Product { get; set; }
public static MediaFile? Create(MediaFileBindingModel? model)
{
if (model == null)
{
return null;
}
return new MediaFile
{
Id = model.Id,
Image = model.Image,
ProductId = model.ProductId,
};
}
public void Update(MediaFileBindingModel? model)
{
if (model == null)
{
return;
}
Image = model.Image;
}
public MediaFileViewModel GetViewModel
{
get
{
var context = new Database();
return new()
{
Id = Id,
Image = Image,
ProductId = ProductId,
ProductName = context.Products.FirstOrDefault(x => x.Id == ProductId)?.Name ?? string.Empty,
};
}
}
}
}