Пример #1
0
        public async Task <IActionResult> Post([FromBody] CreatePostRequest postRequest)
        {
            var post = new Post
            {
                Species     = await _ctx.Species.FirstAsync(s => s.Id == postRequest.Species.Id),
                Breed       = await _ctx.Breeds.FirstAsync(b => b.Id == postRequest.Breed.Id),
                Color       = await _ctx.Colors.FirstAsync(s => s.Id == postRequest.Color.Id),
                Size        = postRequest.Size,
                PostType    = postRequest.PostType,
                LostTime    = postRequest.LostTime,
                Content     = postRequest.Content,
                ImageSource = postRequest.ImageSource,
                Location    = postRequest.Location,
                PostTime    = postRequest.PostTime,
                Author      = await _userManager.FindByEmailAsync(_userHelper.Email)
            };

            _ctx.Attach(post);
            _ctx.Posts.Add(post);
            await _ctx.SaveChangesAsync();

            if (postRequest.ImageSource != null)
            {
                _fileHelper.SaveFile(post.Id, postRequest.ImageSource, ObjectType.Post);
            }

            return(Ok(post.Id));
        }
        public async Task <IActionResult> AddComment([FromBody] Comment comment)
        {
            comment.Author = await _userManager.FindByEmailAsync(_userHelper.Email);

            comment.Post = await _ctx.Posts.FirstOrDefaultAsync(c => c.Id == comment.PostId);

            _ctx.Attach(comment);

            _ctx.Comments.Add(comment);
            await _ctx.SaveChangesAsync();

            return(Ok());
        }