добавила реализация исполнитель дл базы данных. нужно изменить заказы

This commit is contained in:
Елена Бакальская 2024-04-20 18:25:24 +04:00
parent f7478bfdaf
commit a9ea7f8522
3 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1,80 @@
using Microsoft.IdentityModel.Tokens;
using SushiBarContracts.BindingModel;
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarDatabaseImplement.Implements
{
public class ImplementerStorage : IImplementerStorage
{
public ImplementerViewModel? Insert(ImplementerBindingModel model)
{
using var context = new SushiBarDatabase();
var newImplementer = Implementer.Create(model);
if (newImplementer == null) { return null; }
context.Implementers.Add(newImplementer);
context.SaveChanges();
return newImplementer.GetViewModel;
}
public ImplementerViewModel? Update(ImplementerBindingModel model)
{
using var context = new SushiBarDatabase();
var implementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (implementer == null) { return null; };
implementer.Update(model);
context.SaveChanges();
return implementer.GetViewModel;
}
public ImplementerViewModel? Delete(ImplementerBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Implementers.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Implementers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
{
return GetFilteredList(model).FirstOrDefault();
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
using var context = new SushiBarDatabase();
var implementers = context.Implementers.Select(x => x.GetViewModel).ToList();
if (model.Id.HasValue)
{
implementers = implementers.Where(x => x.Id == model.Id.Value).ToList();
}
if (!model.ImplementerFIO.IsNullOrEmpty())
{
implementers = implementers.Where(x => x.ImplementerFIO == model.ImplementerFIO).ToList();
}
if (!model.Password.IsNullOrEmpty())
{
implementers = implementers.Where(x => x.Password == model.Password).ToList();
}
return implementers;
}
public List<ImplementerViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Implementers.Select(x => x.GetViewModel).ToList();
}
}
}

View File

@ -0,0 +1,68 @@
using SushiBarContracts.BindingModel;
using SushiBarContracts.ViewModels;
using SushiBarDataModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarDatabaseImplement.Models
{
public class Implementer : IImplementerModel
{
public int Id { get; private set; }
[Required]
public string ImplementerFIO { get; private set; } = string.Empty;
[Required]
public string Password { get; private set; } = string.Empty;
[Required]
public int WorkExperience { get; private set; }
[Required]
public int Qualification { get; private set; }
[Required]
[ForeignKey("ImplementerId")]
public virtual List<Order> Orders { get; set; } = new();
public static Implementer? Create(ImplementerBindingModel? model)
{
if (model == null)
{
return null;
}
return new Implementer()
{
Id = model.Id,
ImplementerFIO = model.ImplementerFIO,
WorkExperience = model.WorkExperience,
Password = model.Password,
Qualification = model.Qualification,
};
}
public void Update(ImplementerBindingModel? model)
{
if (model == null) return;
ImplementerFIO = model.ImplementerFIO;
WorkExperience = model.WorkExperience;
Password = model.Password;
Qualification = model.Qualification;
}
public ImplementerViewModel GetViewModel => new()
{
Id = Id,
ImplementerFIO = ImplementerFIO,
WorkExperience = WorkExperience,
Password = Password,
Qualification = Qualification,
};
}
}

View File

@ -25,5 +25,6 @@ namespace SushiBarDatabaseImplement
public virtual DbSet<SushiComponent> SushiComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Client> Clients { set; get; }
public virtual DbSet<Implementer> Implementers { set; get; }
}
}