public IActionResult UpdateEmail(UpdateEmailDTO updateEmailDTO)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                {
                    if (context.User.Any(x => x.IsDeleted == false && x.Email == updateEmailDTO.oldEmail))
                    {
                        if (context.User.Any(x => x.IsDeleted == false && x.Email == updateEmailDTO.newEmail))
                        {
                            return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.UserNameOrEmailAlreadyExistError));
                        }
                        else
                        {
                            var User = context.User.FirstOrDefault(x => x.IsDeleted == false && x.Email == updateEmailDTO.oldEmail);
                            User.Email        = updateEmailDTO.newEmail;
                            User.ModifiedDate = DateTime.Now;

                            context.SaveChanges();
                            return(apiJsonResponse.ApiOkContentResult(updateEmailDTO));
                        }
                    }
                    else
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.UserNotFoundError));
                    }
                }
            }
            catch
            { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); }
        }
        public IActionResult UpdatePassword(UpdatePasswordDTO updatePasswordDTO)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                {
                    if (context.User.Any(x => x.IsDeleted == false && x.Username == updatePasswordDTO.Username))
                    {
                        var User = context.User.FirstOrDefault(x => x.IsDeleted == false && x.Username == updatePasswordDTO.Username);
                        if (updatePasswordDTO.NewPassword != string.Empty)
                        {
                            User.Password     = HelperFunctions.ComputeSha256Hash(updatePasswordDTO.NewPassword);
                            User.ModifiedDate = DateTime.Now;
                            context.SaveChanges();
                            return(apiJsonResponse.ApiOkContentResult(updatePasswordDTO));
                        }
                        else
                        {
                            return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.PasswordRequired));
                        }
                    }
                    else
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.UserNotFoundError));
                    }
                }
            }
            catch
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }
        }
        public string Post(model item)
        {
            mymodel a = new mymodel();


            User user = new User();

            user.Name    = item.value;
            user.Surname = item.value + "1";
            a.name       = "Fatih";
            a.surname    = new  string [] { "Cankurtaran", "Cankurtaran2" };
            a.age        = "21";
            using (FoodInfoServiceContext foodInfoServiceContext = new FoodInfoServiceContext())
            {
                foodInfoServiceContext.User.Add(user);
                foodInfoServiceContext.SaveChanges();
            }
            mysecondmodel mysecondmodel = new mysecondmodel();

            mysecondmodel.notlari = "1";
            List <mysecondmodel> mysecondmodels = new List <mysecondmodel>();

            mysecondmodels.Add(mysecondmodel);
            a.mysecondmodels      = mysecondmodels;
            mysecondmodel.notlari = "2";
            mysecondmodels.Add(mysecondmodel);

            var myitem = item;

            return(JsonConvert.SerializeObject(myitem));
        }
        public IActionResult UpdateContent(ContentDTO contentDTO)

        //Product->BarcodeId && Language-> LanguageCode is necessary
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                if (contentDTO != null)
                {
                    using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                    {
                        if (context.ProductContents.Any(x => x.Product.BarcodeId == contentDTO.Product.BarcodeId && x.Language.LanguageCode == contentDTO.Language.LanguageCode && x.IsDeleted == false))
                        {
                            if (contentDTO.NutritionFact != null)
                            {
                                var content = context.ProductContents.Where(x => x.Product.BarcodeId == contentDTO.Product.BarcodeId && x.Language.LanguageCode == contentDTO.Language.LanguageCode && x.IsDeleted == false).FirstOrDefault();



                                contentDTO.NutritionFact.ID = 0;
                                content.NutritionFact       = Mapper.Map <NutritionFacts>(contentDTO.NutritionFact);
                                content.CookingTips         = contentDTO.CookingTips;
                                content.Details             = contentDTO.Details;
                                content.Ingredients         = contentDTO.Ingredients;
                                content.Language            = Mapper.Map <Language>(contentDTO.Language);
                                content.Recommendations     = contentDTO.Recommendations;
                                content.VideoURL            = contentDTO.VideoURL;
                                content.Warnings            = contentDTO.Warnings;
                                content.ModifiedDate        = DateTime.Now;
                                if (contentDTO.ModifiedUserId == null)
                                {
                                    return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ModifiedUserIdRequired));
                                }
                                content.ModifiedUserId = contentDTO.ModifiedUserId;
                                context.SaveChanges();
                                return(apiJsonResponse.ApiOkContentResult(contentDTO));
                            }
                            else
                            {
                                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
                            }
                        }
                        else
                        {
                            return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
                        }
                    }
                }
                else
                {
                    return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
                }
            }
            catch (Exception ex)
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }
        }
        public IActionResult AddCategory(CategoryNameDTO category)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try

            {
                if (category != null)
                {
                    if (category.CategoryName != string.Empty && category.LanguageCode != string.Empty)
                    {
                        using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                        {
                            if (!context.ProductCategories.Any(x => x.CategoryName == category.CategoryName && x.IsDeleted == false && x.LanguageCode == category.LanguageCode))
                            {
                                if (context.Languages.Any(x => x.LanguageCode == category.LanguageCode && x.IsDeleted == false))
                                {
                                    if (category.CreatedUserId == null)
                                    {
                                        return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProvideACreatedUserId));
                                    }
                                    context.ProductCategories.Add(Mapper.Map <ProductCategory>(category));
                                    context.SaveChanges();
                                    var lastItem = context.ProductCategories.Last();
                                    category.ID = lastItem.ID;
                                    category.Id = lastItem.ID;
                                    return(apiJsonResponse.ApiOkContentResult(category));
                                }
                                else
                                {
                                    return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProvideLanguageCode));
                                }
                            }
                            else
                            {
                                return(apiJsonResponse.ApiBadRequestWithMessageAndObject(category, PublicConstants.AlreadyACategoryDefinedWithThisName));
                            }
                        }
                    }


                    else
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProvideCategoryName));
                    }
                }
                else
                {
                    return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
                }
            }

            catch
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }
        }
        public IActionResult SetInitialImagesByBarcodeId(ImageDTO imageDto)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                if (imageDto != null)
                {
                    using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                    {
                        if (context.Products.Any(x => x.BarcodeId == imageDto.BarcodeId && x.IsDeleted == false))
                        {
                            var product = context.Products.FirstOrDefault(x =>
                                                                          x.BarcodeId == imageDto.BarcodeId && x.IsDeleted == false);
                            if (imageDto.FirstImage == null && imageDto.SecondImage == null && imageDto.ThirdImage == null)

                            {
                                return
                                    (apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProvideAtLeastOneImage));
                            }

                            if (imageDto.FirstImage != null)
                            {
                                product.FirstImage = imageDto.FirstImage;
                            }

                            if (imageDto.SecondImage != null)
                            {
                                product.SecondImage = imageDto.SecondImage;
                            }

                            if (imageDto.ThirdImage != null)
                            {
                                product.ThirdImage = imageDto.ThirdImage;
                            }

                            context.SaveChanges();

                            return(apiJsonResponse.ApiOkContentResult());
                        }
                        else
                        {
                            return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProductNotFound));
                        }
                    }
                }
                else
                {
                    return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
                }
            }
            catch (Exception ex)
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }
        }
        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));
            }
        }
Exemplo n.º 8
0
        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 GroupProducts(GroupProductsDTO groupProdutsDTO)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                if (groupProdutsDTO != null)
                {
                    using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                    {
                        var product = context.Products.OrderByDescending(x => x.ProductGroupId).FirstOrDefault();
                        if (groupProdutsDTO.ModifiedUserId == null)
                        {
                            return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ModifiedUserIdRequired));
                        }


                        if (groupProdutsDTO.BarcodeId.Count < 2)
                        {
                            return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.PleaseSelectMoreThanOneProduct));
                        }
                        foreach (var item in groupProdutsDTO.BarcodeId)
                        {
                            var groupedProduct = context.Products.Where(x => x.BarcodeId == item).FirstOrDefault();
                            if (groupedProduct == null)
                            {
                                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProductNotFound));
                            }
                            if (groupedProduct.ProductGroupId != 0)
                            {
                                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProductHasAlreadyGroup));
                            }
                            groupedProduct.ProductGroupId  = product.ProductGroupId + 1;
                            groupedProduct.ModifiedUserId  = groupProdutsDTO.ModifiedUserId;
                            groupedProduct.ModifiedDate    = DateTime.Now;
                            groupProdutsDTO.ProductGroupId = groupedProduct.ProductGroupId;
                        }

                        context.SaveChanges();
                        return(apiJsonResponse.ApiOkContentResult(groupProdutsDTO));
                    }
                }
                else
                {
                    return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
                }
            }
            catch
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }
        }
        public IActionResult UpdateCategoryName(UpdateCategoryNameDTO categoryNameDTO)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                {
                    if (categoryNameDTO != null)
                    {
                        var result = context.ProductCategories.FirstOrDefault(x => x.CategoryName == categoryNameDTO.OldCategoryName);
                        if (result != null)
                        {
                            if (result.IsDeleted == false)
                            {
                                result.ModifiedDate = DateTime.Now;
                                if (categoryNameDTO.ModifiedUserId == null)

                                {
                                    return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProivdeModifiedUserId));
                                }


                                result.ModifiedUserId = categoryNameDTO.ModifiedUserId;
                                result.CategoryName   = categoryNameDTO.NewCategoryName;
                                context.SaveChanges();
                                return(apiJsonResponse.ApiOkContentResult(categoryNameDTO));
                            }
                            else
                            {
                                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.NoCategoryFound));
                            }
                        }
                        else
                        {
                            return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.NoCategoryFound));
                        }
                    }
                    else
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
                    }
                }
            }
            catch (Exception ex)
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }
        }
        public IActionResult DeleteCommentById(DeleteCommentDTO deleteCommentDTO)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                if (deleteCommentDTO != null)
                {
                    if (deleteCommentDTO.ModifiedUserId != null)
                    {
                        using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                        {
                            try
                            {
                                var comment = context.Comments.Where(x => x.ID == deleteCommentDTO.Id && x.IsDeleted == false).FirstOrDefault();
                                comment.IsDeleted      = true;
                                comment.ModifiedDate   = DateTime.Now;
                                comment.ModifiedUserId = deleteCommentDTO.ModifiedUserId;
                            }
                            catch
                            {
                                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.CommentNotFound));
                            }
                            context.SaveChanges();
                            return(apiJsonResponse.ApiOkContentResult(deleteCommentDTO));
                        }
                    }
                    else
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ModifiedUserIdRequired));
                    }
                }
                else
                {
                    return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
                }
            }
            catch
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }
        }
Exemplo n.º 12
0
        public IActionResult SetModeratorByUsername(UserDTO userDTO)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                {
                    var user = context.User.FirstOrDefault(x => x.Username == userDTO.Username && x.IsDeleted == false);

                    if (user == null)
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.UserNotFoundError));
                    }
                    else
                    {
                        user.IsModerator = true;
                        user.IsAdmin     = false;

                        if (userDTO.ModifiedUserId != null)
                        {
                            user.ModifiedUserId = userDTO.ModifiedUserId;
                        }
                        else
                        {
                            return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ModifiedUserIdRequired));
                        }
                        user.ModifiedDate = DateTime.Now;

                        context.SaveChanges();

                        return(apiJsonResponse.ApiOkContentResult(Mapper.Map <ModeratorDTO>(user)));
                    }
                }
            }
            catch (Exception ex)
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }
        }
        public IActionResult DeleteProduct(string BarcodeId)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                {
                    if (BarcodeId != string.Empty)
                    {
                        var product  = context.Products.Where(x => x.BarcodeId == BarcodeId && x.IsDeleted == false).FirstOrDefault();
                        var contents = context.ProductContents.Where(x => x.Product.BarcodeId == BarcodeId && x.IsDeleted == false).ToList();
                        if (contents != null)
                        {
                            foreach (var item in contents)
                            {
                                item.IsDeleted = true;
                            }
                        }
                        if (product != null)
                        {
                            product.IsDeleted = true;
                        }

                        context.SaveChanges();
                        return(apiJsonResponse.ApiOkContentResult(BarcodeId));
                    }
                    else
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.BarcodIdRequired));
                    }
                }
            }
            catch (Exception ex)
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }
        }
        public IActionResult DeleteContent(LanguageAndProductDTO languageAndProductDTO)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                {
                    var product = context.ProductContents.Where(x => x.Product.BarcodeId == languageAndProductDTO.BarcodeId && x.Language.LanguageCode == languageAndProductDTO.LanguageCode && x.IsDeleted == false).FirstOrDefault();
                    if (product == null)
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ProductDoesNotFound));
                    }
                    product.IsDeleted = true;

                    context.SaveChanges();
                    return(apiJsonResponse.ApiOkContentResult(languageAndProductDTO));
                }
            }
            catch (Exception ex)
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }
        }
Exemplo n.º 15
0
        public IActionResult UpdateNameAndSurname(UpdateNameAndSurnameDTO updateNameAndSurnameDTO)
        {
            var apiJsonResult = new ApiJsonResponse();

            try
            {
                using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                {
                    if (context.User.Any(x => x.Username == updateNameAndSurnameDTO.Username && x.IsDeleted == false))
                    {
                        var User = context.User.FirstOrDefault(x => x.Username == updateNameAndSurnameDTO.Username && x.IsDeleted == false);

                        if (updateNameAndSurnameDTO.NewName != string.Empty)
                        {
                            User.Name         = updateNameAndSurnameDTO.NewName;
                            User.ModifiedDate = DateTime.Now;
                        }
                        if (updateNameAndSurnameDTO.NewSurname != string.Empty)
                        {
                            User.Surname      = updateNameAndSurnameDTO.NewSurname;
                            User.ModifiedDate = DateTime.Now;
                        }
                        context.SaveChanges();
                        return(apiJsonResult.ApiOkContentResult(updateNameAndSurnameDTO));
                    }
                    else
                    {
                        return(apiJsonResult.ApiBadRequestWithMessage(PublicConstants.UserNotFoundError));
                    }
                }
            }
            catch
            {
                return(apiJsonResult.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;
            }
        }
        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)); }
            }
        }
Exemplo n.º 18
0
        public IActionResult CreateUser(UserDTO userDTO)
        {
            var apiJsonResponse = new ApiJsonResponse();

            try
            {
                using (FoodInfoServiceContext context = new FoodInfoServiceContext())
                {
                    if (context.User.Any(m => m.Email == userDTO.Email || m.Username == userDTO.Username))
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.UserNameOrEmailAlreadyExistError));
                    }
                    if (userDTO.Username == string.Empty)
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage("Username is required"));
                    }
                    if (userDTO.Email == string.Empty)
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage("Email is required"));
                    }
                    if (userDTO.Password == string.Empty)
                    {
                        return(apiJsonResponse.ApiBadRequestWithMessage("Password is required"));
                    }



                    var hashedPassword = HelperFunctions.ComputeSha256Hash(userDTO.Password);
                    userDTO.Password = hashedPassword;
                    var x = context.User.Add(Mapper.Map <User>(userDTO));

                    //if (!ModelState.IsValid)
                    //{
                    //    return BadRequest(new ApiBadRequestResponse(ModelState));
                    //}
                    context.SaveChanges();
                    UserDTO userCredentialsOnSuccess = new UserDTO();
                    userCredentialsOnSuccess.Name     = userDTO.Name;
                    userCredentialsOnSuccess.Surname  = userDTO.Surname;
                    userCredentialsOnSuccess.Username = userDTO.Username;
                    userCredentialsOnSuccess.Email    = userDTO.Email;
                    return(apiJsonResponse.ApiOkContentResult(userCredentialsOnSuccess));
                    //var isExistUsername = foodInfoServiceContext.User.FirstOrDefault(x => x.Username == "Fatihs");


                    //return Ok(new ApiOkResponse(foodInfoServiceContext.User.FirstOrDefault(x => x.Name == "Fatih")));
                }
            }
            catch (Exception ex)
            {
                return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage));
            }


            //    try
            //    {
            //        if (!ModelState.IsValid)
            //        {
            //            return BadRequest(new ApiBadRequestResponse(ModelState));
            //        }

            //        using (FoodInfoServiceContext foodInfoServiceContext = new FoodInfoServiceContext())
            //        {
            //            var isExistUsername = foodInfoServiceContext.User.FirstOrDefault(x => x.Username == "Fatihs");


            //            return Ok(new ApiOkResponse(foodInfoServiceContext.User.FirstOrDefault(x => x.Name == "Fatih")));

            //        }
            //    }
            //    catch
            //    {
            //        return BadRequest();

            //    }
            //    return BadRequest();
            //}
            //catch
            //{
            //    return Ok();

            //}
            //return BadRequest();
        }