/// <inheritdoc /> public async Task <Result> AddArticle(ArticlesDto articlesDto) { var article = new Articles { Id = articlesDto.Id, Description = articlesDto.Description, HtmlH1 = articlesDto.HtmlH1, MetaDescription = articlesDto.MetaDescription, MetaKeywords = articlesDto.MetaKeywords, MetaTitle = articlesDto.MetaTitle, Name = articlesDto.Name, Sort = articlesDto.Sort, Status = articlesDto.Status }; try { _context.Add(article); _context.SaveChanges(); return(Result.Ok()); } catch (Exception e) { throw new Exception(e.Message); } }
/// <inheritdoc /> public async Task <Result> CreateUser(UserDto dto) { using var transaction = _context.Database.BeginTransaction(); try { var role = await _context.Roles.FindAsync(dto.RoleId); var user = new User { Age = dto.Age, City = dto.City, Email = dto.Email, LastName = dto.LastName, Login = dto.Login, Name = dto.Name, Password = dto.Password, Sort = dto.Sort, Status = dto.Status, Role = role }; _context.Add(user); _context.SaveChanges(); transaction.Commit(); return(Result.Ok()); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }
///<inheritdoc /> public async Task <Result> CreateRole(RoleDto dto) { using var transaction = _context.Database.BeginTransaction(); try { var users = await _context.Users .Where(x => dto.UsersId.Contains(x.Id)) .ToListAsync(); var role = new Role { Name = dto.Name, Sort = dto.Sort, Status = dto.Status, Users = users ?? null }; _context.Add(role); _context.SaveChanges(); transaction.Commit(); return(Result.Ok()); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } }
/// <inheritdoc /> public async Task <Result> AddReview(ReviewDto reviewDto) { try { var product = await _context.Products.FindAsync(reviewDto.ProductId); var review = new Review { Product = product, Author = reviewDto.Author, DateCreate = reviewDto.DateCreate, Status = reviewDto.Status, TextReview = reviewDto.TextReview, Rating = reviewDto.Rating }; _context.Add(review); _context.SaveChanges(); return(Result.Ok()); } catch (Exception e) { Console.WriteLine(e); throw; } }
/// <inheritdoc /> public async Task <Result> AddCategory(CategoryDto categoryDto) { try { var picture = new Picture(); if (categoryDto.Pictures != null) { picture = await GetFile(categoryDto.Pictures); } var parentCategory = _context.Categories.Find(categoryDto.ParentCategoryId); var category = new Category { Description = categoryDto.Description, HtmlH1 = categoryDto.HtmlH1, MetaDescription = categoryDto.MetaDescription, MetaKeywords = categoryDto.MetaKeywords, MetaTitle = categoryDto.MetaTitle, Name = categoryDto.Name, Sort = categoryDto.Sort, Status = categoryDto.Status }; _context.Add(category); _context.SaveChanges(); if (parentCategory != null && parentCategory.Id != category.Id) { var categoryToCategory = new CategoryCategory { Category1 = category, Category1Id = category.Id, Category2 = parentCategory, Category2Id = parentCategory.Id }; _context.Add(categoryToCategory); _context.SaveChanges(); } var categoryToPicture = new CategoryPicture { Category = category, CategoryId = category.Id, Picture = picture }; _context.Add(categoryToPicture); _context.SaveChanges(); return(Result.Ok()); } catch (Exception e) { throw new ApplicationException(e.Message); } }
/// <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 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); } }
public ActionResult Create([Bind(Include = "ButtonID,Label,Style,URI,Response,TimeOut,Users")] Button button) { //ViewBag.responselist = MyMethods.ResponseTypes(value["responseID"]); if (ModelState.IsValid) { db.Buttons.Add(button); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(button)); }
/// <inheritdoc /> public async Task <Result> AddBlogArticle(BlogArticlesDto blogArticlesDto) { try { var file = new Picture(); if (blogArticlesDto.Picture != null) { file = await GetFile(blogArticlesDto.Picture); } var recomendedProducts = await _context.Products .Where(x => blogArticlesDto.RecomendedProductsId.Contains(x.Id)).ToListAsync(); var blogArticles = await _context.BlogArticles .Where(x => blogArticlesDto.BlogArticlesId.Contains(x.Id)).ToListAsync(); var blogCategory = _context.BlogCategory.Find(blogArticlesDto.BlogCategoryId); var blogArticle = new BlogArticle { Description = blogArticlesDto.Description, HtmlH1 = blogArticlesDto.HtmlH1, MetaDescription = blogArticlesDto.MetaDescription, MetaTitle = blogArticlesDto.MetaTitle, Name = blogArticlesDto.Name, MetaKeywords = blogArticlesDto.MetaKeywords, Sort = blogArticlesDto.Sort, Status = blogArticlesDto.Status, RecomendedProducts = recomendedProducts, BlogArticles = blogArticles, BlogCategory = blogCategory, Picture = file }; _context.Add(blogArticle); _context.SaveChanges(); return(Result.Ok()); } catch (Exception e) { throw new ApplicationException(e.Message); } }
/// <summary> /// Удалить Выбранные элементы. /// </summary> /// <param name="ids">Список идентификаторов.</param> protected bool DeleteItems(IEnumerable <long> ids) { try { _set.RemoveRange(_set.Where(x => ids.Contains(x.Id))); _context.SaveChanges(); return(true); } catch (Exception e) { return(false); } }
/// <inheritdoc /> public async Task <Result> SaveVideo(SaveLinkVideoDto saveLinkVideoDto) { await using var transaction = _context.Database.BeginTransaction(); try { if (saveLinkVideoDto.ProductId != null && saveLinkVideoDto.Videos.Count > 0) { var videos = saveLinkVideoDto.Videos.Select(x => new Video { CategoryId = x.ChanelId, Link = x.Video, PlaylistId = x.PlaylistId, Title = x.Title }); _context.AddRange(videos); _context.SaveChanges(); var product = await _context.Products.FindAsync(saveLinkVideoDto.ProductId); var videoProduct = videos.Select(video => new VideoProduct { Product = product, ProductId = product.Id, Video = video, VideoId = video.Id }); _context.AddRange(videoProduct); _context.SaveChanges(); transaction.Commit(); return(Result.Ok()); } return(Result.Fail( $"Запись видео не удалась. Отсутствует товар с таким идентификатором: {saveLinkVideoDto.ProductId}")); } catch (Exception e) { await transaction.RollbackAsync(); throw; } }
/// <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 async Task <Result> AddProduct(ProductDto productDto) { try { var picture = new Picture(); var listPicture = new List <Picture>(); if (productDto.Picture != null) { picture = await GetFile(productDto.Picture); } if (productDto.Files != null && productDto.Files.Count > 0) { listPicture = productDto.Files.Select(file => GetFile(file).Result).ToList(); } var manufacturer = await _context.Manufacturers.FindAsync(productDto.ManufacturerId); var listRecomendedProducts = await _context.Products .Where(x => productDto.RecomendedProductsId.Contains(x.Id)) .ToListAsync(); var category = await _context.Categories.FindAsync(productDto.CategoryId); var product = GetNewProduct(productDto, picture, manufacturer, category); _context.Add(product); _context.SaveChanges(); var productPictures = GetProductPictureList(listPicture, product); _context.AddRange(productPictures); _context.SaveChanges(); var listProductProduct = GetProductProductList(listRecomendedProducts, product); _context.AddRange(listProductProduct); _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); } }