Exemplo n.º 1
0
        public async Task <ActionResult <Data.Models.RefreshToken> > UpdateRefreshToken(int tokenId, Data.Models.RefreshTokenForUpdate updatedRefreshToken)
        {
            try
            {
                Data.Entities.RefreshToken dbRefreshToken = await _repository.GetRefreshTokenAsync(tokenId);

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

                _mapper.Map(updatedRefreshToken, dbRefreshToken);
                if (await _repository.SaveChangesAsync())
                {
                    Data.Models.RefreshToken savedRefreshToken = _mapper.Map <Data.Models.RefreshToken>(dbRefreshToken);
                    return(Ok(savedRefreshToken));
                }
                else
                {
                    return(BadRequest("Failed to update."));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database exception: " + ex.Message));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <Data.Models.RefreshToken> > PatchRefreshToken(int tokenId, JsonPatchDocument <Data.Models.RefreshTokenForUpdate> patchDocument)
        {
            try
            {
                Data.Entities.RefreshToken dbRefreshToken = await _repository.GetRefreshTokenAsync(tokenId);

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

                var updatedRefreshToken = _mapper.Map <Data.Models.RefreshTokenForUpdate>(dbRefreshToken);
                patchDocument.ApplyTo(updatedRefreshToken, ModelState);

                _mapper.Map(updatedRefreshToken, dbRefreshToken);

                if (await _repository.SaveChangesAsync())
                {
                    Data.Models.RefreshToken savedRefreshToken = _mapper.Map <Data.Models.RefreshToken>(await _repository.GetRefreshTokenAsync(tokenId));
                    return(Ok(savedRefreshToken));
                }
                else
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to save to database"));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to patch refreshToken " + ex.Message));
            }
        }
Exemplo n.º 3
0
        public async Task <ActionResult <Data.Models.RefreshToken> > CreateNewRefreshToken(Data.Models.RefreshTokenForCreate newRefreshToken)
        {
            Data.Entities.RefreshToken dbNewRefreshToken = null;
            try
            {
                dbNewRefreshToken = _mapper.Map <Data.Entities.RefreshToken>(newRefreshToken);
            }
            catch (Exception ex)
            {
                return(BadRequest("Input is in invalid format: " + ex.Message));
            }

            if (dbNewRefreshToken == null)
            {
                return(BadRequest("Input is in invalid format"));
            }

            await _repository.AddAsync <Data.Entities.RefreshToken>(dbNewRefreshToken);

            await _repository.SaveChangesAsync();

            Data.Models.RefreshToken addedRefreshToken = _mapper.Map <Data.Models.RefreshToken>(dbNewRefreshToken);

            var url = _linkgenerator.GetPathByAction(HttpContext, "GetRefreshTokenByTokenId", "RefreshTokens", addedRefreshToken);

            return(this.Created(url, addedRefreshToken));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> DeleteRefreshToken(int tokenId)
        {
            try
            {
                Data.Entities.RefreshToken dbRefreshToken = await _repository.GetRefreshTokenAsync(tokenId);

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

                _repository.Delete <Data.Entities.RefreshToken>(dbRefreshToken);
                await _repository.SaveChangesAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database exception: " + ex.Message));
            }
        }