Exemplo n.º 1
0
        public async Task <IActionResult> Post(WineryDTO request)
        {
            try
            {
                var response = await wineryRepository.AddWineryAsync(request.MapToWineryContract());

                if (response.Result != Guid.Empty)
                {
                    request.Id = response.Result;
                    return(Created($"api/winery/{request.Id}", request));
                }
                else
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to Add Winery!"));
                }
            }
            catch (Exception ex)
            {
                if (ex is FormatException)
                {
                    return(StatusCode(StatusCodes.Status400BadRequest, ex.Message));
                }
                else
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
                }
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Patch(Guid wineryId, [FromBody] JsonPatchDocument <WineryDTO> wineryPatch)
        {
            try
            {
                var winery = await wineryRepository.WineryExistsAsync(
                    new Request <Guid>
                {
                    Data = wineryId
                });

                if (!winery.Result)
                {
                    return(NotFound($"Winery with Id {wineryId} does not exists."));
                }

                WineryDTO wineryDTO = new WineryDTO();
                wineryPatch.ApplyTo(wineryDTO);

                return(Ok(wineryDTO));
            }
            catch (Exception ex)
            {
                if (ex is FormatException)
                {
                    return(StatusCode(StatusCodes.Status400BadRequest, ex.Message));
                }
                else
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
                }
            }
        }
Exemplo n.º 3
0
 public static Contract.Winery MapToWineryContract(this WineryDTO winery)
 {
     return(new Contract.Winery
     {
         Id = winery.Id,
         Name = winery.Name,
         Region = winery.Region,
         Country = winery.Country
     });
 }
Exemplo n.º 4
0
        public async Task <IActionResult> Put(WineryDTO request)
        {
            try
            {
                var winery = await wineryRepository.WineryExistsAsync(
                    new Request <Guid>
                {
                    Data = request.Id
                });

                if (!winery.Result)
                {
                    return(NotFound($"Winery with Id {request.Id} does not exists."));
                }

                var response = await wineryRepository.UpdateWineryAsync(request.MapToWineryContract());

                if (response.Result > 0)
                {
                    return(Ok(request));
                }
                else
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to Update Winery!"));
                }
            }
            catch (Exception ex)
            {
                if (ex is FormatException)
                {
                    return(StatusCode(StatusCodes.Status400BadRequest, ex.Message));
                }
                else
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
                }
            }
        }