public async Task <CatlogDto> Handle(Command request, CancellationToken cancellationToken) { //var test = request.test; var catalog = await _context.Catalogs .FindAsync(request.Id); if (catalog == null) { throw new RestException(HttpStatusCode.NotFound, new { Catalog = "Not found" }); } var CurrentUsername = _userAccessor.GetCurrentUsername(); float price = 0; if (!float.TryParse(request.Price, out price)) { throw new Exception("invalid Price"); } if (CurrentUsername.ToLower().ToString() == "admin" || catalog.Supplier.UserName == CurrentUsername) { var category = await _context.Categories.SingleOrDefaultAsync(x => x.Id == request.CategoryId); #region CategoryColores //delete all existing entries _context.CategoryColores.RemoveRange(catalog.Colores); //Add existing entries if (request.Colores != null) { foreach (string c in request.Colores) { var color = new CategoryColores { configid = c }; catalog.Colores.Add(color); } } #endregion CategoryColores #region Sizes //delete all existing entries _context.CategorySize.RemoveRange(catalog.Sizes); // Add existing entries if (request.Sizes != null) { foreach (CategorySize s in request.Sizes) { var size = new CategorySize { configid = s.configid, Title = s.Title, Qty = s.Qty }; catalog.Sizes.Add(size); } } #endregion Sizes if (category != null) { catalog.Category = category; } catalog.DisplayName = request.DisplayName ?? catalog.DisplayName; catalog.Description = request.Description ?? catalog.Description; catalog.Price = price; // _context.Entry(cl).State = EntityState.Modified; //.Entry(user).State = EntityState.Added; / var success = await _context.SaveChangesAsync() > 0; //if (success) return Unit.Value; if (success) { var toReturn = _mapper.Map <Catalog, CatlogDto>(catalog); return(toReturn); } } else { throw new Exception("Unauthorized access"); } throw new Exception("Problem saving changes"); }
public async Task <CatlogDto> Handle(Command request, CancellationToken cancellationToken) { var supplier = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername()); var category = await _context.Categories.SingleOrDefaultAsync(x => x.Id == request.CategoryId); //CategoryId float price = 0; if (!float.TryParse(request.Price, out price)) { throw new Exception("invalid Price"); } var cl = new List <CategoryColores>(); //Add existing entries if (request.Colores != null) { if (request.Colores != null) { foreach (string c in request.Colores) { var color = new CategoryColores { //Id = Guid.NewGuid(), //CatlogId = request.Id, configid = c }; cl.Add(color); } } } var cz = new List <CategorySize>(); if (request.Sizes != null) { foreach (CategorySize s in request.Sizes) { var size = new CategorySize { configid = s.configid, Title = s.Title, Qty = s.Qty }; //{ // Id = Guid.NewGuid(), // Title = s.Title, // Qty = s.Qty //}; cz.Add(size); } } var catalog = new Catalog { //Id = request.Id, DisplayName = request.DisplayName, Description = request.Description, Supplier = supplier, Category = category, Price = price, Colores = cl, Sizes = cz }; _context.Catalogs.Add(catalog); var success = await _context.SaveChangesAsync() > 0; if (success) { var toReturn = _mapper.Map <Catalog, CatlogDto>(catalog); return(toReturn); } //return Unit.Value; //var toReturn = _mapper.Map<Catalog, CatlogDto>(catalog); //return toReturn; throw new Exception("Problem saving changes"); }