Ejemplo n.º 1
0
        // POST: api/friend
        public async Task <IActionResult> Create([FromBody] AddFriendModel friend)
        {
            try
            {
                var userId = _userManager.GetUserId(User);
                if (userId == null)
                {
                    _logger.LogError($"Error inside FriendController Create action: UserId not found");
                    return(Unauthorized("You must be logged in to add a friend"));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError($"Error inside FriendController Create action: AddFriendModel not valid");
                    return(BadRequest("Invalid model object"));
                }

                var newFriend = _mapper.Map <Friend>(friend);
                newFriend.UserId = userId; //userId isn't added from the client side model

                _repoWrapper.Friends.Create(newFriend);
                _repoWrapper.UnitOfWorkComplete();

                return(CreatedAtRoute("GetFriend", new { id = newFriend.Id }, newFriend));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error inside FriendController Create action: {ex.Message}");
                return(StatusCode(500, "Internal Sever Error"));
            }
        }