Ejemplo n.º 1
0
        public async Task <IActionResult> Create(int id, [FromForm] PostIncoming postIncoming)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }


            var postToCreate = _mapper.Map <Post>(postIncoming);

            if (postIncoming.Images != null)
            {
                var images = new List <Image>();

                foreach (var i in postIncoming.Images)
                {
                    images.Add(await UploadPhoto(i));
                }
                postToCreate.Images = images;
            }

            _repo.CreatePost(id, postToCreate);

            if (!await _repo.SaveAll())
            {
                throw new Exception("Failed");
            }

            postToCreate.Category = JsonConvert.DeserializeObject <Category>(postIncoming.Category);

            _repo.UpdatePost(postToCreate);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception("Failed");
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> UpdatePost(int id, [FromForm] PostIncoming postIncoming)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var postFromRepo = await _repo.GetPost(postIncoming.Id);

            if (postFromRepo == null)
            {
                throw new Exception("Post not found");
            }

            if (postFromRepo.User.Id != id)
            {
                return(Unauthorized());
            }


            var images = new List <Image>();

            if (postIncoming.ImagesToSave != null || (postFromRepo.Images.Count != 0 && postIncoming.ImagesToSave == null))
            {
                if (postIncoming.ImagesToSave != null)
                {
                    postIncoming.ImagesToSave.ForEach(
                        (jimg) => {
                        images.Add(JsonConvert.DeserializeObject <Image>(jimg));
                    }
                        );
                }
                _repo.DeleteAllImages(postIncoming.Id);
            }

            if (postIncoming.Images != null)
            {
                foreach (var i in postIncoming.Images)
                {
                    images.Add(await UploadPhoto(i));
                }
            }

            postFromRepo.Category = JsonConvert.DeserializeObject <Category>(postIncoming.Category);

            postFromRepo.Images  = images;
            postFromRepo.Content = postIncoming.Content;
            _repo.UpdatePost(postFromRepo);
            //_mapper.Map(postIncoming, postFromRepo);


            // if(images.Count > 0){
            //     images.ForEach(
            //         (img) => {
            //             _repo.AddImage(postIncoming.Id, img);
            //         }
            //     );
            // }

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception("Updating. failed on save");
        }