/// <inheritdoc /> public async Task <Result> AddCategory(BlogCategoryDto blogCategoryDto) { await using (var transaction = _context.Database.BeginTransaction()) { try { var picture = new Picture(); if (blogCategoryDto.Picture != null) { picture = await GetFile(blogCategoryDto.Picture); } var parentBlogCategory = _context.BlogCategory.Find(blogCategoryDto.BlogCategoryId); var blogCategory = GetBlogCategory(blogCategoryDto, picture); _context.Add(blogCategory); SaveInformationBlogCategory2BlogCategory(parentBlogCategory, blogCategory); await transaction.CommitAsync(); return(Result.Ok()); } catch (Exception e) { transaction.Rollback(); throw new ApplicationException(e.InnerException.Message ?? e.Message); } } }
/// <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 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> 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> 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> 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> 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 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); } }