Пример #1
0
        private void ModifyFriend(Guid id, FriendRequest request)
        {
            var friend = _context.Friends.Find(id);

            friend = _mapper.Map(request, friend);

            _context.Friends.Update(friend);
            _context.SaveChanges();
        }
Пример #2
0
        private FriendResponse CreateFriend(FriendRequest friendRequest)
        {
            var friend = _mapper.Map <Friend>(friendRequest);

            friend.Id = Guid.NewGuid();

            _context.Friends.Add(friend);
            _context.SaveChanges();

            return(_mapper.Map <FriendResponse>(friend));
        }
Пример #3
0
        public ActionResult Post([FromBody] FriendRequest friendRequest)
        {
            var errors = friendRequest.Errors();

            if (errors.Any())
            {
                return(UnprocessableEntity(errors)); //422
            }

            var response = CreateFriend(friendRequest);

            return(CreatedAtAction(nameof(Get), new { response.Id }, response)); //201
        }
Пример #4
0
        public ActionResult Put(Guid id, [FromBody] FriendRequest request)
        {
            var response = SearchFriendById(id);

            if (response == null)
            {
                return(NotFound()); //404
            }
            else
            {
                ModifyFriend(id, request);
                return(NoContent()); //204
            }
        }