Хранилище Егора вроде бы готово

This commit is contained in:
GokaPek 2024-04-22 21:01:26 +04:00
parent 3f9d9ce648
commit 7f5cd4b778
8 changed files with 217 additions and 12 deletions

View File

@ -8,6 +8,7 @@ namespace UniversityContracts.SearchModels
{ {
public class DisciplineSearchModel public class DisciplineSearchModel
{ {
public int? Id { get; set; }
public int? TeacherId { get; set; } public int? TeacherId { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public string? Description { get; set; } public string? Description { get; set; }

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityDataModels;
using UniversityDataModels.Models;
namespace UniversityContracts.SearchModels
{
public class StorekeeperSearchModel
{
public int? Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? MiddleName { get; set; }
public string? PhoneNumber { get; set; }
public string? Email { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.StorageContracts
{
public interface IStorekeeperStorage
{
List<StorekeeperViewModel> GetFullList();
List<StorekeeperViewModel> GetFilteredList(StorekeeperSearchModel model);
StorekeeperViewModel? GetElement(StorekeeperSearchModel model);
StorekeeperViewModel? Insert(StorekeeperBindingModel model);
StorekeeperViewModel? Update(StorekeeperBindingModel model);
StorekeeperViewModel? Delete(StorekeeperBindingModel model);
}
}

View File

@ -1,12 +1,93 @@
using System; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection.Metadata;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StorageContracts;
using UniversityContracts.ViewModels;
using UniversityDatabaseImplement.Models;
namespace UniversityDatabaseImplement.Implements namespace UniversityDatabaseImplement.Implements
{ {
public class DisciplineStorage public class DisciplineStorage : IDisciplineStorage
{ {
public DisciplineViewModel? Delete(DisciplineBindingModel model)
{
using var context = new UniversityDatabase();
var element = context.Disciplines.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Disciplines.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public DisciplineViewModel? GetElement(DisciplineSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new UniversityDatabase();
return context.Disciplines
.Include(x => x.Teacher)
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel;
}
public List<DisciplineViewModel> GetFilteredList(DisciplineSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) || string.IsNullOrEmpty(model.Description))
{
return new();
}
using var context = new UniversityDatabase();
return context.Disciplines
.Where(x => x.Name.Contains(model.Name) || x.Description.Contains(model.Description) || x.Id == model.Id || x.TeacherId == model.TeacherId)
.Include(x => x.Teacher)
.Select(x => x.GetViewModel)
.ToList();
}
public List<DisciplineViewModel> GetFullList()
{
using var context = new UniversityDatabase();
return context.Disciplines
.Select(x => x.GetViewModel)
.ToList();
}
public DisciplineViewModel? Insert(DisciplineBindingModel model)
{
using var context = new UniversityDatabase();
var newDiscipline = Discipline.Create(model);
if (newDiscipline == null)
{
return null;
}
context.Disciplines.Add(newDiscipline);
context.SaveChanges();
return newDiscipline.GetViewModel;
}
public DisciplineViewModel? Update(DisciplineBindingModel model)
{
using var context = new UniversityDatabase();
var discipline = context.Disciplines.FirstOrDefault(x => x.Id == model.Id);
if (discipline == null)
{
return null;
}
discipline.Update(model);
context.SaveChanges();
return discipline.GetViewModel;
}
} }
} }

View File

@ -3,10 +3,84 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StorageContracts;
using UniversityContracts.ViewModels;
using UniversityDatabaseImplement.Models;
using UniversityDataModels.Models;
namespace UniversityDatabaseImplement.Implements namespace UniversityDatabaseImplement.Implements
{ {
public class StorekeeperStorage public class StorekeeperStorage: IStorekeeperStorage
{ {
public StorekeeperViewModel? GetElement(StorekeeperSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new UniversityDatabase();
return context.Storekeepers.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<StorekeeperViewModel> GetFilteredList(StorekeeperSearchModel model)
{
if (string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new UniversityDatabase();
return context.Storekeepers
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
public List<StorekeeperViewModel> GetFullList()
{
using var context = new UniversityDatabase();
return context.Storekeepers.Select(x => x.GetViewModel).ToList();
}
public StorekeeperViewModel? Insert(StorekeeperBindingModel model)
{
var newStorekeeper = Storekeeper.Create(model);
if (newStorekeeper == null)
{
return null;
}
using var context = new UniversityDatabase();
context.Storekeepers.Add(newStorekeeper);
context.SaveChanges();
return newStorekeeper.GetViewModel;
}
public StorekeeperViewModel? Update(StorekeeperBindingModel model)
{
using var context = new UniversityDatabase();
var client = context.Storekeepers.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
public StorekeeperViewModel? Delete(StorekeeperBindingModel model)
{
using var context = new UniversityDatabase();
var client = context.Storekeepers.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
context.Storekeepers.Remove(client);
context.SaveChanges();
return client.GetViewModel;
}
} }
} }

View File

@ -1,4 +1,5 @@
using System; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -8,7 +9,7 @@ using UniversityContracts.SearchModels;
using UniversityContracts.StorageContracts; using UniversityContracts.StorageContracts;
using UniversityContracts.ViewModels; using UniversityContracts.ViewModels;
using UniversityDatabaseImplement.Models; using UniversityDatabaseImplement.Models;
// TODO
namespace UniversityDatabaseImplement.Implements namespace UniversityDatabaseImplement.Implements
{ {
public class TeacherStorage: ITeacherStorage public class TeacherStorage: ITeacherStorage
@ -22,26 +23,29 @@ namespace UniversityDatabaseImplement.Implements
} }
public List<TeacherViewModel> GetFilteredList(TeacherSearchModel model) public List<TeacherViewModel> GetFilteredList(TeacherSearchModel model)
{ {
if (string.IsNullOrEmpty(model.Name)) if (string.IsNullOrEmpty(model.Name) || string.IsNullOrEmpty(model.AcademicDegree) || string.IsNullOrEmpty(model.Position))
{ {
return new(); return new();
} }
using var context = new UniversityDatabase(); using var context = new UniversityDatabase();
return context.Teachers return context.Teachers
.Where(x => x.Name.Contains(model.Name)) .Where(x => x.Name.Contains(model.Name))
.Where(x => x.Position.Contains(model.Position))
.Where(x => x.AcademicDegree.Contains(model.AcademicDegree))
.Include(x => x.Storekeeper)
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
public TeacherViewModel? GetElement(TeacherSearchModel model) public TeacherViewModel? GetElement(TeacherSearchModel model)
{ {
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) if (string.IsNullOrEmpty(model.Name) && string.IsNullOrEmpty(model.AcademicDegree) && string.IsNullOrEmpty(model.Position) && !model.Id.HasValue)
{ {
return null; return null;
} }
using var context = new UniversityDatabase(); using var context = new UniversityDatabase();
return context.Teachers return context.Teachers
.FirstOrDefault(x => // стоит ли проверять по Position и Degree?
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
} }
public TeacherViewModel? Insert(TeacherBindingModel model) public TeacherViewModel? Insert(TeacherBindingModel model)
{ {

View File

@ -22,7 +22,7 @@ namespace UniversityDatabaseImplement.Models
public string Description { get; private set; } = string.Empty; public string Description { get; private set; } = string.Empty;
public virtual Teacher Teacher { get; set; } = new(); public virtual Teacher Teacher { get; set; } = new();
[ForeignKey("DisciplineId")] [ForeignKey("DisciplineId")]
public virtual List<StudentDiscipline> StudentDiscipline { get; set; } = new(); public virtual List<StudentDiscipline> StudentDisciplines { get; set; } = new();
public static Discipline? Create(DisciplineBindingModel model) public static Discipline? Create(DisciplineBindingModel model)
{ {
if (model == null) if (model == null)

View File

@ -25,9 +25,9 @@ namespace UniversityDatabaseImplement.Models
public string Position { get; private set; } = string.Empty; public string Position { get; private set; } = string.Empty;
public virtual Storekeeper Storekeeper { get; set; } = new (); public virtual Storekeeper Storekeeper { get; set; } = new ();
[ForeignKey("TeacherId")] [ForeignKey("TeacherId")]
public virtual List<Statement> Statement { get; set; } = new(); public virtual List<Statement> Statements { get; set; } = new();
[ForeignKey("TeacherId")] [ForeignKey("TeacherId")]
public virtual List<Discipline> Discipline { get; set; } = new(); public virtual List<Discipline> Disciplines { get; set; } = new();
public static Teacher? Create(TeacherBindingModel model) public static Teacher? Create(TeacherBindingModel model)
{ {
if (model == null) if (model == null)