示例#1
0
        public async Task <PostDTO> UpdatePost(string postId, CreatingPostDTO model)
        {
            var post = await _dbContext.Posts.FirstOrDefaultAsync(p => p.PostID.ToString() == postId);

            if (null != postId)
            {
                post.Address       = model.Address;
                post.Price         = model.Price;
                post.NumberLodgers = model.NumberLodgers;
                post.Description   = model.Description;
                post.LastUpdate    = DateTime.Now;

                try
                {
                    _dbContext.SaveChanges();

                    return(_modelFactory.ConvertToPostDTO(post));
                }
                catch (Exception e)
                {
                    return(null);
                }
            }

            return(null);
        }
示例#2
0
        public async Task <PostDTO> CreatePost(CreatingPostDTO model)
        {
            var user = await _userManager.FindByIdAsync(model.PhongTroUserID);

            if (null != user)
            {
                var post = new Post()
                {
                    PostID         = Guid.NewGuid(),
                    Address        = model.Address,
                    Price          = model.Price,
                    NumberLodgers  = model.NumberLodgers,
                    Description    = model.Description,
                    PhongTroUserID = user.Id,
                };

                try
                {
                    _dbContext.Posts.Add(post);

                    Array.ForEach(model.Images, img =>
                    {
                        _dbContext.BoardingHouseImages.Add(new BoardingHouseImage()
                        {
                            BoardingHouseImageID = Guid.NewGuid(),
                            Post = post,
                            Url  = img
                        });
                    });

                    _dbContext.SaveChanges();

                    return(_modelFactory.ConvertToPostDTO(post));
                }
                catch (Exception e)
                {
                    string content = e.ToString();
                    return(null);
                }
            }

            return(null);
        }
示例#3
0
        public async Task <IHttpActionResult> CreatePost(CreatingPostDTO model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            model.PhongTroUserID = User.Identity.GetUserId();
            var resultPost = await _Repository.CreatePost(model);

            if (null == resultPost)
            {
                return(InternalServerError());
            }

            Uri locationHeader = new Uri(Url.Link("GetPostById", new { id = resultPost.Id }));

            return(Created(locationHeader, resultPost));
        }
示例#4
0
        public async Task <IHttpActionResult> UpdatePost([FromUri] string PostId, [FromBody] CreatingPostDTO model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userId = User.Identity.GetUserId();
            var post   = await _Repository.GetPostOfUserById(userId, PostId);

            if (null == post)
            {
                return(NotFound());
            }

            var resultPost = await _Repository.UpdatePost(PostId, model);

            if (null == resultPost)
            {
                return(InternalServerError());
            }

            return(Ok(resultPost));
        }