This commit is contained in:
Николай 2023-04-01 21:25:50 +04:00
parent 7eb1515a4d
commit a31d5543ec
7 changed files with 43 additions and 10 deletions

View File

@ -10,5 +10,7 @@ namespace HardwareShopContracts.BindingModels
public string Text { get; set; } = string.Empty;
public int BuildId { get; set; }
public int UserId { get; set; }
}
}

View File

@ -8,5 +8,7 @@ namespace HardwareShopContracts.SearchModels
public int? Id { get; set; }
public int? BuildId { get; set; }
public int? UserId { get; set; }
}
}

View File

@ -14,5 +14,10 @@ namespace HardwareShopContracts.ViewModels
public string BuildName { get; set; } = string.Empty;
public int BuildId { get; set; }
public int UserId { get; set; }
[DisplayName("Email клиента")]
public string UserEmail { get; set; } = string.Empty;
}
}

View File

@ -11,5 +11,7 @@ namespace HardwareShopDataModels.Models
string Text { get; }
int BuildId { get; }
int UserId { get; }
}
}

View File

@ -24,7 +24,7 @@ namespace HardwareShopContracts.StoragesContracts
public List<BuildViewModel> GetFilteredList(BuildSearchModel model)
{
if (model.Id.HasValue)
if (!model.Id.HasValue)
{
return new();
}

View File

@ -14,19 +14,31 @@ namespace HardwareShopContracts.StoragesContracts
using var context = new HardwareShopDatabase();
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.Select(x => x.GetViewModel)
.ToList();
}
public List<CommentViewModel> GetFilteredList(CommentSearchModel model)
{
if (model.BuildId.HasValue)
if (!model.Id.HasValue)
{
return new();
}
using var context = new HardwareShopDatabase();
if (model.UserId.HasValue)
{
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.Where(x => x.BuildId == model.BuildId)
.Select(x => x.GetViewModel)
.ToList();
@ -34,13 +46,14 @@ namespace HardwareShopContracts.StoragesContracts
public CommentViewModel? GetElement(CommentSearchModel model)
{
if (model.Id.HasValue)
if (!model.Id.HasValue)
{
return null;
}
using var context = new HardwareShopDatabase();
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
@ -61,17 +74,17 @@ namespace HardwareShopContracts.StoragesContracts
public CommentViewModel? Update(CommentBindingModel model)
{
using var context = new HardwareShopDatabase();
var comment = context.Comments.FirstOrDefault(x => x.Id == model.Id);
var comment = context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id);
if (comment == null)
{
return null;
}
comment.Update(model);
context.SaveChanges();
return context.Comments
.Include(x => x.Build)
.FirstOrDefault(x => x.Id == comment.Id)
?.GetViewModel;
return comment.GetViewModel;
}
public CommentViewModel? Delete(CommentBindingModel model)
@ -79,6 +92,7 @@ namespace HardwareShopContracts.StoragesContracts
using var context = new HardwareShopDatabase();
var element = context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{

View File

@ -18,6 +18,11 @@ namespace HardwareShopDatabaseImplement.Models.Worker
public virtual Build Build { get; set; }
[Required]
public int UserId { get; set; }
public virtual User User { get; set; }
public static Comment? Create(CommentBindingModel? model)
{
if (model == null)
@ -28,7 +33,8 @@ namespace HardwareShopDatabaseImplement.Models.Worker
{
Id = model.Id,
Text = model.Text,
BuildId = model.BuildId
BuildId = model.BuildId,
UserId = model.UserId,
};
}
public void Update(CommentBindingModel? model)
@ -44,7 +50,9 @@ namespace HardwareShopDatabaseImplement.Models.Worker
Id = Id,
Text = Text,
BuildId = BuildId,
BuildName = Build.BuildName
BuildName = Build.BuildName,
UserId = UserId,
UserEmail = User.Email,
};
}
}