Лучший способ реализовать систему корзины оформления заказа в ASP.NET MVC [закрыто]C#

Место общения программистов C#
Anonymous
Лучший способ реализовать систему корзины оформления заказа в ASP.NET MVC [закрыто]

Сообщение Anonymous »

Итак, я работаю над групповым проектом, к сожалению, мой партнер не проверил код перед его отправкой, и он изначально сломался.
Произошло, когда пользователь купил набор и часть, которая была в наборе.
(Таким образом, набор с 10 скелетами, тогда пользователь уже покупает скелет в этом наборе)
Это выдаст ошибку из-за того, что ядро EF обнаружит несколько основных ключей.
(User Pieces — это составной ключ PieceId и UserId).
Мое простое решение заключалось в том, чтобы просто сохранить его после проверки частей и последующего просмотра наборов.
Есть ли лучший способ сделать это?
public void CheckoutCart(int userId)
{
try
{
// Get pieces from the cart
var cartItems = this.GetCartItems(userId);
foreach (var item in cartItems)
{
// If user doesn't already own it, add it
if (!this._dataContext.UserPieces.Any(up => up.UserId == userId && up.PieceId == item.PieceId))
{
this._dataContext.UserPieces.Add(new UserPieces { UserId = userId, PieceId = item.PieceId });
}
// Archive the cart item
item.IsArchived = true;
this._dataContext.Update(item);
}
// https://stackoverflow.com/questions/274 ... -framework
this._dataContext.SaveChanges();
this._dataContext.ChangeTracker.Clear();

// Get sets from the cart
// We need to retrieve the sets with the PiecesList included to get the PieceIds
var cartSets = this._dataContext.CartItemSets
.Where(cis => cis.UserId == userId && cis.IsArchived == false)
.Include(cis => cis.Set)
.ThenInclude(s => s!.PiecesList)
.ToList();

foreach (var setItem in cartSets)
{
// Add all pieces from the set to the user account
if (setItem.Set != null && setItem.Set.PiecesList != null)
{
foreach (var pieceSet in setItem.Set.PiecesList)
{
if (!this._dataContext.UserPieces.Any(up => up.UserId == userId && up.PieceId == pieceSet.PieceId))
{
this._dataContext.UserPieces.Add(new UserPieces { UserId = userId, PieceId = pieceSet.PieceId });
}
}
}
setItem.IsArchived = true;
this._dataContext.Update(setItem);
}

this._dataContext.SaveChanges();
}

Вернуться в «C#»