2024-11-06 01:27:34 +04:00
|
|
|
|
using CandidateReviewContracts.BindingModels;
|
|
|
|
|
using CandidateReviewContracts.SearchModels;
|
|
|
|
|
using CandidateReviewContracts.StoragesContracts;
|
|
|
|
|
using CandidateReviewContracts.ViewModels;
|
|
|
|
|
using CandidateReviewDatabaseImplement.Models;
|
2024-11-23 21:39:18 +04:00
|
|
|
|
using CandidateReviewDataModels.Enums;
|
2024-11-06 01:27:34 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-11-04 22:26:40 +04:00
|
|
|
|
|
|
|
|
|
namespace CandidateReviewDatabaseImplement.Implements
|
|
|
|
|
{
|
2024-11-06 01:27:34 +04:00
|
|
|
|
public class UserStorage : IUserStorage
|
2024-11-04 22:26:40 +04:00
|
|
|
|
{
|
2024-11-06 01:27:34 +04:00
|
|
|
|
public UserViewModel? Delete(UserBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CandidateReviewDatabase();
|
|
|
|
|
var element = context.Users.Include(x => x.Company).Include(x => x.Resumes).Include(x => x.Assessments).FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Users.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserViewModel? GetElement(UserSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new CandidateReviewDatabase();
|
|
|
|
|
return context.Users
|
|
|
|
|
.Include(x => x.Company)
|
|
|
|
|
.Include(x => x.Resumes)
|
|
|
|
|
.Include(x => x.Assessments)
|
|
|
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) || (model.Id.HasValue && x.Id == model.Id))?
|
|
|
|
|
.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<UserViewModel> GetFilteredList(UserSearchModel model)
|
|
|
|
|
{
|
2024-11-23 21:39:18 +04:00
|
|
|
|
using var context = new CandidateReviewDatabase();
|
|
|
|
|
|
|
|
|
|
if (model.CompanyId.HasValue && model.Role != null)
|
|
|
|
|
{
|
|
|
|
|
return context.Users
|
|
|
|
|
.Include(x => x.Company)
|
|
|
|
|
.Include(x => x.Resumes)
|
|
|
|
|
.Include(x => x.Assessments)
|
|
|
|
|
.Where(x => x.CompanyId.Equals(model.CompanyId) && x.Role.Equals(model.Role))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (string.IsNullOrEmpty(model.Email) || string.IsNullOrEmpty(model.Password))
|
2024-11-06 01:27:34 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
2024-11-23 21:39:18 +04:00
|
|
|
|
|
2024-11-06 01:27:34 +04:00
|
|
|
|
return context.Users
|
|
|
|
|
.Include(x => x.Company)
|
|
|
|
|
.Include(x => x.Resumes)
|
|
|
|
|
.Include(x => x.Assessments)
|
|
|
|
|
.Where(x => x.Email.Equals(model.Email) && x.Password.Equals(model.Password))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<UserViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new CandidateReviewDatabase();
|
|
|
|
|
return context.Users
|
|
|
|
|
.Include(x => x.Company)
|
|
|
|
|
.Include(x => x.Resumes)
|
|
|
|
|
.Include(x => x.Assessments)
|
|
|
|
|
.ToList()
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserViewModel? Insert(UserBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var newUser = User.Create(model);
|
|
|
|
|
if (newUser == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new CandidateReviewDatabase();
|
|
|
|
|
context.Users.Add(newUser);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return context.Users
|
|
|
|
|
.Include(x => x.Company)
|
|
|
|
|
.Include(x => x.Resumes)
|
|
|
|
|
.Include(x => x.Assessments)
|
|
|
|
|
.FirstOrDefault(x => x.Id == newUser.Id)?
|
|
|
|
|
.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserViewModel? Update(UserBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CandidateReviewDatabase();
|
|
|
|
|
var user = context.Users
|
|
|
|
|
.Include(x => x.Company)
|
|
|
|
|
.Include(x => x.Resumes)
|
|
|
|
|
.Include(x => x.Assessments)
|
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return user.GetViewModel;
|
|
|
|
|
}
|
2024-11-04 22:26:40 +04:00
|
|
|
|
}
|
|
|
|
|
}
|