Beispiel #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");
        }
 /// <summary>
 /// create the post.
 /// </summary>
 /// <param name="model">the post edit add model.</param>
 /// <returns>the task.</returns>
 public async Task CreatePost(PostEditAddModel model)
 {
     var entity = _mapper.Map <PostEditAddModel, Post>(model);
     await _postRepository.CreatePost(entity).ConfigureAwait(false);
 }