コード例 #1
0
        public async Task <IActionResult> Post([FromBody] FavoriteBuildingDto favoriteBuildingDto)
        {
            //Get header token
            if (Request.Headers.TryGetValue("Authorization", out StringValues headerValues))
            {
                var token = _customEncoder.DecodeBearerAuth(headerValues.First());
                if (token != null)
                {
                    var user = await _userService.GetUserAsyncByToken(token);

                    if (user != null)
                    {
                        //Verify if the token exist and is not expire
                        if ((await _authenticationService.CheckIfTokenIsValidAsync(token) && user.IsAdmin == 1) || await _authenticationService.CheckIfTokenIsValidAsync(token, user.UserId))
                        {
                            var favoriteBuildingApiDto = await _buildingService.PostFavoriteBuildingAsync(favoriteBuildingDto);

                            if (favoriteBuildingApiDto == null)
                            {
                                return(StatusCode(404, "Unable to create favorite building."));
                            }
                            return(Ok(favoriteBuildingApiDto));
                        }
                        return(StatusCode(401, "Invalid token."));
                    }
                    return(StatusCode(403, "Invalid user."));
                }
                return(StatusCode(401, "Invalid authorization."));
            }
            return(StatusCode(401, "Invalid authorization."));
        }
コード例 #2
0
 /// <summary>
 /// Posts the favorite building async.
 /// </summary>
 /// <returns>The favorite building async.</returns>
 /// <param name="favoriteBuildingDto">Favorite building dto.</param>
 public async Task <FavoriteBuildingApiDto> PostFavoriteBuildingAsync(FavoriteBuildingDto favoriteBuildingDto)
 {
     return(await _dal.PostFavoriteBuildingAsync(favoriteBuildingDto));
 }