public IActionResult AddComment(CommentDTO commentDTO) { var apiJsonResponse = new ApiJsonResponse(); try { if (commentDTO != null) { if (commentDTO.UserComment == string.Empty) { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.CommentCanNotBeEmpty)); } else { using (FoodInfoServiceContext context = new FoodInfoServiceContext()) { Comment comment = new Comment(); try { comment.ProductContent = context.ProductContents.Where(x => x.ID == commentDTO.ProductContentId) .Include(m => m.Product) .Include(m => m.NutritionFact) .Include(m => m.Language) .Include(m => m.NutritionFact) .FirstOrDefault(); } catch { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProductContentIdRequired)); } comment.UserComment = commentDTO.UserComment; if (commentDTO.CreatedUserId == null) { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProvideACreatedUserId)); } comment.CreatedUserId = commentDTO.CreatedUserId; context.Add(comment); context.SaveChanges(); return(apiJsonResponse.ApiOkContentResult(commentDTO)); } } } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } } catch { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } }
public IActionResult AddVote(VoteDTO voteDTO) { var apiJsonResponse = new ApiJsonResponse(); try { if (voteDTO != null) { if (voteDTO.UserVote != null && voteDTO.CreatedUserId != null && voteDTO.BarcodeID != string.Empty) { using (FoodInfoServiceContext context = new FoodInfoServiceContext()) { if (context.Products.Any(x => x.BarcodeId == voteDTO.BarcodeID && x.IsDeleted == false)) { var vote = new Vote(); vote.Product = context.Products.Where(x => x.BarcodeId == voteDTO.BarcodeID && x.IsDeleted == false).FirstOrDefault(); if (context.Votes.Any(x => x.CreatedUserId == voteDTO.CreatedUserId && x.Product.ID == vote.Product.ID && vote.IsDeleted == false)) { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.UserAlreadyVoteThisProduct)); } vote.UserVote = voteDTO.UserVote; vote.CreatedUserId = voteDTO.CreatedUserId; context.Add(vote); context.SaveChanges(); return(apiJsonResponse.ApiOkContentResult(voteDTO)); } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProductNotFound)); } } } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } } catch { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } }
public IActionResult CreateContentOfProduct(ContentDTO contentDTO) { var apiJsonResponse = new ApiJsonResponse(); try { using (FoodInfoServiceContext context = new FoodInfoServiceContext()) { if (contentDTO != null) { if (contentDTO.Product.BarcodeId != null) { if (contentDTO.CreatedUserId != null) { using (var transaction = context.Database.BeginTransaction()) { if (context.Products.Any(x => x.BarcodeId == contentDTO.Product.BarcodeId && x.IsDeleted == false)) { contentDTO.Product.ID = context.Products.FirstOrDefault(x => x.BarcodeId == contentDTO.Product.BarcodeId && x.IsDeleted == false).ID; } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.BarcodIdRequired)); } if (context.Languages.Any(x => x.LanguageCode == contentDTO.Language.LanguageCode && x.IsDeleted == false)) { contentDTO.Language.ID = context.Languages.FirstOrDefault(x => x.LanguageCode == contentDTO.Language.LanguageCode && x.IsDeleted == false).ID; } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProvideLanguageCode)); } if (context.ProductContents.Any(x => x.Product.ID == contentDTO.Product.ID && x.IsDeleted == false && x.Language.ID == contentDTO.Language.ID)) { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ExistingContentForProduct)); } // contentDTO.NutritionFact.ID = context.NutritionFacts.FirstOrDefault(x => x.ID == contentDTO.NutritionFact.ID && x.IsDeleted == false).ID; var nutritionFacts = new NutritionFacts(); nutritionFacts = Mapper.Map <NutritionFacts>(contentDTO.NutritionFact); nutritionFacts.BarcodeId = contentDTO.Product.BarcodeId; nutritionFacts.CreatedUserId = contentDTO.CreatedUserId; nutritionFacts.LanguageCode = contentDTO.Language.LanguageCode; context.Add(nutritionFacts); context.SaveChanges(); contentDTO.NutritionFact.ID = context.NutritionFacts.Where(x => x.BarcodeId == contentDTO.Product.BarcodeId && x.LanguageCode == contentDTO.Language.LanguageCode && x.IsDeleted == false).FirstOrDefault().ID; var content = context.ProductContents.Add(Mapper.Map <ProductContent>(contentDTO)); context.SaveChanges(); transaction.Commit(); return(apiJsonResponse.ApiOkContentResult(Mapper.Map <ProductContent>(contentDTO))); } } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProvideACreatedUserId)); } } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.BarcodIdRequired)); } } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } } } catch (Exception ex) { { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } } }
public IActionResult CreateProduct(ProductDTO productDTO) { var apiJsonResponse = new ApiJsonResponse(); try { using (FoodInfoServiceContext context = new FoodInfoServiceContext()) { if (productDTO != null) { if (productDTO.BarcodeId == string.Empty && productDTO.ProductName == string.Empty) { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } else { if (!context.Products.Any(x => x.BarcodeId == productDTO.BarcodeId && x.IsDeleted == false)) { var product = new Product(); //product.BarcodeId = productDTO.BarcodeId; //product.ProductName = productDTO.ProductName; //product.ProductCategory = productDTO.ProductCategory; //product.CreatedUserId = productDTO.CreatedUserId; product = Mapper.Map <Product>(productDTO); if (productDTO.FirstImage != null) { product.FirstImage = productDTO.FirstImage; } else { product.FirstImage = null; } if (productDTO.SecondImage != null) { product.SecondImage = productDTO.SecondImage; } else { product.SecondImage = null; } if (productDTO.ThirdImage != null) { product.ThirdImage = productDTO.ThirdImage; } else { product.ThirdImage = null; } if (!context.ProductCategories.Any(x => x.ID == productDTO.ProductCategory.ID && x.IsDeleted == false)) { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.NoCategoryFound)); } product.ProductCategory = context.ProductCategories.Where(x => x.ID == productDTO.ProductCategory.ID && x.IsDeleted == false).FirstOrDefault(); context.Add(product); context.SaveChanges(); return(apiJsonResponse.ApiOkContentResult(productDTO)); } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } } } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } } } catch (Exception ex) { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); throw; } }