Хранилище Егора вроде бы готово
This commit is contained in:
parent
3f9d9ce648
commit
7f5cd4b778
@ -8,6 +8,7 @@ namespace UniversityContracts.SearchModels
|
||||
{
|
||||
public class DisciplineSearchModel
|
||||
{
|
||||
public int? Id { get; set; }
|
||||
public int? TeacherId { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Description { get; set; }
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -1,12 +1,93 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityContracts.BindingModels;
|
||||
using UniversityContracts.SearchModels;
|
||||
using UniversityContracts.StorageContracts;
|
||||
using UniversityContracts.ViewModels;
|
||||
using UniversityDatabaseImplement.Models;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,84 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
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
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -8,7 +9,7 @@ using UniversityContracts.SearchModels;
|
||||
using UniversityContracts.StorageContracts;
|
||||
using UniversityContracts.ViewModels;
|
||||
using UniversityDatabaseImplement.Models;
|
||||
// TODO
|
||||
|
||||
namespace UniversityDatabaseImplement.Implements
|
||||
{
|
||||
public class TeacherStorage: ITeacherStorage
|
||||
@ -22,26 +23,29 @@ namespace UniversityDatabaseImplement.Implements
|
||||
}
|
||||
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();
|
||||
}
|
||||
using var context = new UniversityDatabase();
|
||||
return context.Teachers
|
||||
.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)
|
||||
.ToList();
|
||||
}
|
||||
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;
|
||||
}
|
||||
using var context = new UniversityDatabase();
|
||||
return context.Teachers
|
||||
.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
// стоит ли проверять по Position и Degree?
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
public TeacherViewModel? Insert(TeacherBindingModel model)
|
||||
{
|
||||
|
@ -22,7 +22,7 @@ namespace UniversityDatabaseImplement.Models
|
||||
public string Description { get; private set; } = string.Empty;
|
||||
public virtual Teacher Teacher { get; set; } = new();
|
||||
[ForeignKey("DisciplineId")]
|
||||
public virtual List<StudentDiscipline> StudentDiscipline { get; set; } = new();
|
||||
public virtual List<StudentDiscipline> StudentDisciplines { get; set; } = new();
|
||||
public static Discipline? Create(DisciplineBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
|
@ -25,9 +25,9 @@ namespace UniversityDatabaseImplement.Models
|
||||
public string Position { get; private set; } = string.Empty;
|
||||
public virtual Storekeeper Storekeeper { get; set; } = new ();
|
||||
[ForeignKey("TeacherId")]
|
||||
public virtual List<Statement> Statement { get; set; } = new();
|
||||
public virtual List<Statement> Statements { get; set; } = new();
|
||||
[ForeignKey("TeacherId")]
|
||||
public virtual List<Discipline> Discipline { get; set; } = new();
|
||||
public virtual List<Discipline> Disciplines { get; set; } = new();
|
||||
public static Teacher? Create(TeacherBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
|
Loading…
Reference in New Issue
Block a user