Exemplo n.º 1
0
        public IActionResult Put(int id, [FromBody] MateItem mateItem)
        {
            var     userClaims = User.Claims;
            var     UserId     = userClaims.Where(cl => cl.Type == "user_id").FirstOrDefault().Value;
            AppUser appUser    = _dbContext.AppUsers.Where(appusr => appusr.UniqueId == UserId).FirstOrDefault();

            MateItem ExistingmateItem = _dbContext.MateItems.Where(mi => mi.Id == id && mi.Owner == appUser).FirstOrDefault();

            if (ExistingmateItem == null)
            {
                return(NotFound($"Could not found an item you are looking for(Are you sure you are the owner of the item?)"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest("Submitted wrong data!"));
            }

            ////////////////////////////////////////////////////////////////////////////////////////
            ExistingmateItem.Title        = mateItem.Title ?? ExistingmateItem.Title;
            ExistingmateItem.Description  = mateItem.Description ?? ExistingmateItem.Description;
            ExistingmateItem.ItemCategory = mateItem.ItemCategory ?? ExistingmateItem.ItemCategory;
            ExistingmateItem.ItemState    = mateItem.ItemState ?? ExistingmateItem.ItemState;
            ExistingmateItem.Owner        = mateItem.Owner ?? ExistingmateItem.Owner;
            ///////////////////////////////////////////////////////////////////////////////////////

            _dbContext.Entry(ExistingmateItem).State = EntityState.Modified;
            _dbContext.SaveChanges();

            string newUri = Url.Link("GetMateItem", new { id = ExistingmateItem.Id });

            return(Created(newUri, ExistingmateItem));
        }
        public IActionResult EditSpecificUserPost(int mateItemId, MateItem mateItem)
        {
            var userClaims = User.Claims;
            var UserId     = userClaims.Where(cl => cl.Type == "user_id").FirstOrDefault().Value;
            var appUser    = _dbContext.AppUsers.Where(appusr => appusr.UniqueId == UserId).FirstOrDefault();

            MateItem ExistingmateItem = _dbContext.MateItems.Where(mi => mi.AppUserId == appUser.Id && mi.Id == mateItemId).FirstOrDefault();

            if (ExistingmateItem == null)
            {
                return(NotFound($"Could not found an item you are looking for(Are you sure you are the owner of the item?)"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest("Submitted wrong data!"));
            }

            ////////////////////////////////////////////////////////////////////////////////////////
            ExistingmateItem.Title        = mateItem.Title ?? ExistingmateItem.Title;
            ExistingmateItem.Description  = mateItem.Description ?? ExistingmateItem.Description;
            ExistingmateItem.ItemCategory = mateItem.ItemCategory ?? ExistingmateItem.ItemCategory;
            ExistingmateItem.ItemState    = mateItem.ItemState ?? ExistingmateItem.ItemState;

            //The user has tamperded with his ad, so set it's approved property to false until it has been thorougly checked.
            ExistingmateItem.Approved = false;


            //////////////Now new photos----------Insert them--------//////////////////////////////
            var uploadsFolderPath = Path.Combine(_hostingEnv.WebRootPath, "Uploads");

            if (!Directory.Exists(uploadsFolderPath))
            {
                Directory.CreateDirectory(uploadsFolderPath);
            }

            List <IFormFile> frmfiles = new List <IFormFile>();

            if (mateItem.FirstImage != null && mateItem.FirstImage.Length > 0 && mateItem.FirstImage.Length < Utilities.MAX_FILE_SIZE)
            {
                if (Utilities.ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(mateItem.FirstImage.FileName)))
                {
                    frmfiles.Add(mateItem.FirstImage);
                }
            }
            if (mateItem.SecondImage != null && mateItem.SecondImage.Length > 0 && mateItem.SecondImage.Length < Utilities.MAX_FILE_SIZE)
            {
                if (Utilities.ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(mateItem.SecondImage.FileName)))
                {
                    frmfiles.Add(mateItem.SecondImage);
                }
            }
            if (mateItem.ThirdImage != null && mateItem.ThirdImage.Length > 0 && mateItem.ThirdImage.Length < Utilities.MAX_FILE_SIZE)
            {
                if (Utilities.ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(mateItem.ThirdImage.FileName)))
                {
                    frmfiles.Add(mateItem.ThirdImage);
                }
            }

            if (frmfiles.Count > 0)
            {
                foreach (var item in frmfiles)
                {
                    var firstImgFileName = Guid.NewGuid().ToString() + Path.GetExtension(item.FileName);
                    var firstImgfilePath = Path.Combine(uploadsFolderPath, firstImgFileName);
                    //using (var stream = new FileStream(firstImgfilePath, FileMode.Create))
                    //{
                    //    item.CopyTo(stream);
                    //}

                    using (var img = Image.FromStream(item.OpenReadStream()))
                    {
                        Stream memStrm     = new MemoryStream(img.Resize(300, 200).ToByteArray());
                        var    filestrmRes = new FileStreamResult(memStrm, "image/jpg");
                        var    stream      = new FileStream(firstImgfilePath, FileMode.Create);
                        try
                        {
                            memStrm.CopyTo(stream);
                            //filestrmRes.FileStream.CopyTo(memStrm);
                        }
                        finally
                        {
                            stream.Dispose();
                        }
                    }

                    var newPhoto = new Photo
                    {
                        Filename = firstImgFileName
                    };

                    ExistingmateItem.ItemPhotos.Add(newPhoto);
                }
            }

            ///////////////////////////////////////////////////////////////////////////////////////

            _dbContext.Entry(ExistingmateItem).State = EntityState.Modified;
            _dbContext.SaveChanges();

            //Send email to the user and inform him that his ad has been disabled while update to his ad is beign inspected, and will be enabled if it passes necessary inspection. Also notify the admin to check and approve/dissaprove this post.
            NotifyAdminAndPostOwnerOfUpdatedPost(ExistingmateItem);

            return(Ok(ExistingmateItem));

            //string newUri = Url.Link("GetMateItem", new { id = ExistingmateItem.Id });
            //return Created(newUri, ExistingmateItem);
        }