Exemple #1
0
        public async Task <Offer> EditOfferAsync(EditOfferModel offerInput, string id)
        {
            var offer = await this.context.Offers.FirstOrDefaultAsync(x => x.Id == id);

            if (offer != null)
            {
                offer.Name        = offerInput.Name;
                offer.Price       = offerInput.Price;
                offer.Description = offerInput.Description;
                offer.ModifiedOn  = DateTime.UtcNow;
                offer.CategoryId  = offerInput.CategoryId;
                offer.BuyContent  = offerInput.BuyContent;

                if (offerInput.Photo != null)
                {
                    var photoUrl = await this.cloudinaryService.UploadPhotoAsync(
                        offerInput.Photo,
                        $"{id}-{DateTime.Now.ToString()}",
                        GlobalConstants.CloudFolderForProfilePictures);

                    offer.PicUrl = photoUrl;
                }

                await this.context.SaveChangesAsync();

                return(offer);
            }

            return(null);
        }
Exemple #2
0
        public async Task <IActionResult> Edit(string id, EditOfferModel inputModel) // index post requsest for create
        {
            if (!this.ModelState.IsValid)
            {
                return(this.Redirect($"/Offer/Edit?id={id}"));
            }

            var offerInput = inputModel;
            var imgSize    = offerInput.Photo != null ? inputModel.Photo.Length : 1;

            if (imgSize >= 1048576)
            {
                return(this.Redirect($"/Offer/Edit?id={id}"));
            }

            var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (!await this.offerService.IsUserCreatorOfOfferAsync(userId, id))
            {
                return(this.Redirect($"/Offer/Details?id={id}"));
            }

            var categoryId = await this.categoryService.GetIdByNameAsync(offerInput.CategoryName);

            offerInput.CategoryId = categoryId;

            if (imgSize >= 1048576)
            {
                return(this.Redirect($"/Offer/Edit?id={id}"));
            }

            await this.offerService.EditOfferAsync(offerInput, id);

            return(this.Redirect("/"));
        }