/// <inheritdoc /> public Result UpdateArticle(ArticlesDto articlesDto) { using var transaction = _context.Database.BeginTransaction(); try { var article = _context.Articleses.Find(articlesDto.Id); article.Description = articlesDto.Description; article.HtmlH1 = articlesDto.HtmlH1; article.MetaDescription = articlesDto.MetaDescription; article.MetaKeywords = articlesDto.MetaKeywords; article.MetaTitle = articlesDto.MetaTitle; article.Name = articlesDto.Name; article.Sort = articlesDto.Sort; article.Status = articlesDto.Status; _context.Update(article); _context.SaveChanges(); transaction.Commit(); return(Result.Ok()); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }
/// <inheritdoc /> public Result UpdateUser(UserDto dto) { using var transaction = _context.Database.BeginTransaction(); try { var role = _context.Roles.Find(dto.RoleId); var user = _context.Users.FirstOrDefault(x => x.Id == dto.Id); if (user != null) { user.Age = dto.Age; user.City = dto.City; user.Email = dto.Email; user.LastName = dto.LastName; user.Login = dto.Login; user.Name = dto.Name; user.Password = dto.Password; user.Sort = dto.Sort; user.Status = dto.Status; user.Role = role; _context.Update(user); _context.SaveChanges(); } transaction.Commit(); return(Result.Ok()); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }
/// <inheritdoc /> public Result UpdateBlogRewiew(BlogRewiewDto blogRewiewDto) { using var transaction = _context.Database.BeginTransaction(); try { var review = _context.BlogReviews.Include(x => x.BlogArticle) .FirstOrDefault(x => x.Id == blogRewiewDto.Id); var blogArticle = _context.BlogArticles.Find(blogRewiewDto.BlogArticleId); if (review != null) { review.BlogArticle = blogArticle; review.Author = blogRewiewDto.Author; review.DateCreate = blogRewiewDto.DateCreate; review.Rating = blogRewiewDto.Rating; review.Status = blogRewiewDto.Status; review.TextReview = blogRewiewDto.TextReview; _context.Update(review); _context.SaveChanges(); transaction.Commit(); return(Result.Ok()); } return(Result.Fail("Ошибка при обновлении отзыва.")); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }
/// <inheritdoc /> public Result UpdateCustomer(CustomerDto customerDto) { using var transaction = _context.Database.BeginTransaction(); try { var customer = _context.Customers.Find(customerDto.Id); customer.Address = customerDto.Address; customer.City = customerDto.City; customer.Company = customerDto.Company; customer.Country = customerDto.Country; customer.Email = customerDto.Email; customer.Faxs = customerDto.Faxs; customer.Ip = customerDto.Ip; customer.LastName = customerDto.LastName; customer.Name = customerDto.Name; customer.Phone = customerDto.Phone; customer.Region = customerDto.Region; customer.Sort = customerDto.Sort; customer.Status = customerDto.Status; customer.Zip = customerDto.Zip; _context.Update(customer); _context.SaveChanges(); transaction.Commit(); return(Result.Ok()); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }
///<inheritdoc /> public Result UpdateRole(RoleDto dto) { using var transaction = _context.Database.BeginTransaction(); try { var users = _context.Users .Where(x => dto.UsersId.Contains(x.Id)) .ToList(); var role = _context.Roles.Find(dto.Id); if (role != null) { role.Name = dto.Name; role.Sort = dto.Sort; role.Status = dto.Status; role.Users = users ?? null; _context.Update(role); } _context.SaveChanges(); transaction.Commit(); return(Result.Ok()); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }
/// <inheritdoc /> public Result UpdateProduct(ProductDto productDto) { using var transaction = _context.Database.BeginTransaction(); try { var picture = GetFile(productDto.Picture).Result; var listPicture = productDto.Files.Select(file => GetFile(file).Result).ToList(); var manufacturer = _context.Manufacturers.Find(productDto.ManufacturerId); var listRecomendedProducts = _context.Products .Where(x => productDto.RecomendedProductsId.Contains(x.Id)) .ToList(); var category = _context.Categories.Find(productDto.CategoryId); var product = _context.Products.Find(productDto.Id); product.Sort = productDto.Sort; product.Status = productDto.Status; product.Name = productDto.Name; product.Count = productDto.Count; product.DataEndStock = productDto.DataEndStock; product.DataStartStock = productDto.DataStartStock; product.FullDescription = productDto.FullDescription; product.HtmlH1 = productDto.HtmlH1; product.MetaDescription = productDto.MetaDescription; product.MetaKeywords = productDto.MetaKeywords; product.MetaTitle = productDto.MetaTitle; product.Price = productDto.Price; product.SmallDescription = productDto.SmallDescription; product.Stock = productDto.Stock; product.Tags = productDto.Tags; product.Weight = productDto.Weight; product.Picture = picture; product.Manufacturer = manufacturer; product.Categories = category; _context.Update(product); _context.SaveChanges(); var listProductProduct = GetProductProductList(listRecomendedProducts, product); _context.AddRange(listProductProduct); _context.SaveChanges(); var productPictures = GetProductPictureList(listPicture, product); _context.AddRange(productPictures); _context.SaveChanges(); transaction.Commit(); return(Result.Ok()); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }
/// <inheritdoc /> public Result UpdateCategory(CategoryDto categoryDto) { var category = _context.Categories.Find(categoryDto.Id); var parentCategory = _context.Categories.Find(categoryDto.ParentCategoryId); var picture = GetFile(categoryDto.Pictures).Result; category.Description = categoryDto.Description; category.HtmlH1 = categoryDto.HtmlH1; category.MetaDescription = categoryDto.MetaDescription; category.MetaKeywords = categoryDto.MetaKeywords; category.MetaTitle = categoryDto.MetaTitle; category.Name = categoryDto.Name; category.Sort = categoryDto.Sort; category.Status = categoryDto.Status; _context.Update(category); _context.SaveChanges(); if (parentCategory != null && parentCategory.Id != category.Id) { var categoryParent = _context.CategoryCategory.FirstOrDefault(x => x.Category1.Id == category.Id); if (categoryParent != null) { _context.Remove(categoryParent); _context.SaveChanges(); } var categoryToCategory = new CategoryCategory { Category1 = category, Category1Id = category.Id, Category2 = parentCategory, Category2Id = parentCategory.Id }; _context.Add(categoryToCategory); _context.SaveChanges(); } var pictureDelete = _context.CategoryPicture.FirstOrDefault(x => x.CategoryId == category.Id); var pictureRemove = _context.Pictures.FirstOrDefault(x => x.Id == pictureDelete.PictureId); _context.Remove(pictureDelete ?? throw new ApplicationException()); _context.Remove(pictureRemove ?? throw new ApplicationException()); _context.SaveChanges(); var categoryToPicture = new CategoryPicture { Category = category, CategoryId = category.Id, Picture = picture }; _context.Add(categoryToPicture); _context.SaveChanges(); return(Result.Ok()); }
/// <inheritdoc /> public Result UpdateOrder(OrderDto orderDto) { using var transaction = _context.Database.BeginTransaction(); try { var customer = _context.Customers.Find(orderDto.CustomerId); var productsQueryNew = _context.Products.Where(x => orderDto.ProductsId.Contains(x.Id)).ToList(); var order = _context.Orders.Include(x => x.Products).FirstOrDefault(x => x.Id == orderDto.Id); if (order != null) { order.Customer = customer ?? null; order.Sum = orderDto.Sum; order.TimeAdd = orderDto.TimeAdd; order.TimeUpdate = orderDto.TimeUpdate; _context.Update(order); _context.SaveChanges(); if (productsQueryNew.Count > 0) { var oldProduct = order.Products; if (oldProduct.Count > 0) { _context.RemoveRange(oldProduct); } var products = productsQueryNew.Select(x => new OrderProduct { OrderId = order.Id, Order = order, Product = x, ProductId = x.Id }); _context.AddRange(products); _context.SaveChanges(); } transaction.Commit(); return(Result.Ok()); } return(Result.Fail("Обновление не увенчалось успехом.")); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }
/// <inheritdoc /> public Result UpdateBlogArticle(BlogArticlesDto blogArticlesDto) { using var transaction = _context.Database.BeginTransaction(); try { var picture = GetFile(blogArticlesDto.Picture).Result; var blogCategory = _context.BlogCategory.Find(blogArticlesDto.BlogCategoryId); var blogArticles = new List <BlogArticle>(); if (blogArticlesDto.BlogArticlesId.Length > 0) { blogArticles = _context.BlogArticles.Where(x => blogArticlesDto.BlogArticlesId.Contains(x.Id)) .ToList(); } var blogArticle = _context.BlogArticles.Find(blogArticlesDto.Id); blogArticle.Description = blogArticlesDto.Description; blogArticle.HtmlH1 = blogArticlesDto.HtmlH1; blogArticle.MetaDescription = blogArticlesDto.MetaDescription; blogArticle.MetaKeywords = blogArticlesDto.MetaKeywords; blogArticle.MetaTitle = blogArticlesDto.MetaTitle; blogArticle.Name = blogArticlesDto.Name; blogArticle.Sort = blogArticlesDto.Sort; blogArticle.Status = blogArticlesDto.Status; blogArticle.BlogCategory = blogCategory; blogArticle.Picture = picture; blogArticle.BlogArticles = blogArticles; _context.Update(blogArticle); _context.SaveChanges(); transaction.Commit(); return(Result.Ok()); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }
/// <inheritdoc /> public Result UpdateManufucturer(ManufacturerDto manufacturerDto) { try { var manufacturer = _context.Manufacturers.Find(manufacturerDto.Id); manufacturer.Name = manufacturerDto.Name; manufacturer.Sort = manufacturerDto.Sort; manufacturer.Status = manufacturerDto.Status; _context.Update(manufacturer); _context.SaveChanges(); return(Result.Ok()); } catch (Exception e) { throw new ApplicationException(e.Message); } }
/// <inheritdoc /> public Result UpdateCategory(BlogCategoryDto blogCategoryDto) { using var transaction = _context.Database.BeginTransaction(); try { var category = _context.BlogCategory.Include(x => x.Picture) .FirstOrDefault(x => x.Id == blogCategoryDto.Id); var file = GetFile(blogCategoryDto.Picture).Result; var parentCategory = _context.BlogCategory.Find(blogCategoryDto.BlogCategoryId); if (category != null) { category.Picture = file; category.Description = blogCategoryDto.Description; category.HtmlH1 = blogCategoryDto.HtmlH1; category.MetaDescription = blogCategoryDto.MetaDescription; category.MetaKeywords = blogCategoryDto.MetaKeywords; category.MetaTitle = blogCategoryDto.MetaTitle; category.Name = blogCategoryDto.Name; category.Sort = blogCategoryDto.Sort; category.Status = blogCategoryDto.Status; _context.Update(category); if (parentCategory != null) { var blogCategory = _context.BlogCategory2BlogCategories.FirstOrDefault(x => x.BlogCategory1.Id == category.Id); if (blogCategory != null) { _context.Remove(blogCategory); } var newBlogCategory = new BlogCategory2BlogCategory { BlogCategory1 = category, BlogCategory1Id = category.Id, BlogCategory2Id = parentCategory.Id, BlogCategory2 = parentCategory }; _context.Add(newBlogCategory); _context.SaveChanges(); } transaction.Commit(); return(Result.Ok()); } return(Result.Fail("Обновление категории не произошло.")); } catch (Exception e) { transaction.RollbackAsync(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }