Esempio n. 1
0
        public async Task <IActionResult> EditItem(int itemId, [FromBody] EditItemRequestModel model)
        {
            Guard.Argument(itemId, nameof(itemId)).GreaterThan(0);
            Guard.Argument(model, nameof(model)).NotNull();

            // Validate security hash provided in the request
            bool isValidSecurityHash = SecurityHashValidator.IsValidSecurityHash(model.UserId, model.SecurityHash);

            // If the security hash is invalid it means it's been tempered with, so we terminate the request
            if (!isValidSecurityHash)
            {
                return(BadRequest(new BaseApiResponse <ItemServiceModel>
                {
                    StatusCode = StatusCodes.Status400BadRequest,
                    Message = ResponseMessages.BadRequest,
                }));
            }

            ItemServiceModel item = await this._itemService.GetItemAsync(itemId);

            if (item == null)
            {
                return(Ok(new BaseApiResponse <ItemServiceModel>
                {
                    StatusCode = StatusCodes.Status404NotFound,
                    Message = ResponseMessages.NotFound,
                }));
            }

            var editItemServiceModel = this._mapper.Map <EditItemServiceModel>(model);

            bool result = await this._itemService.Edit(editItemServiceModel);

            if (!result)
            {
                return(Ok(new BaseApiResponse <ItemServiceModel>
                {
                    StatusCode = StatusCodes.Status500InternalServerError,
                    Message = ResponseMessages.ServerError,
                }));
            }

            item = await this._itemService.GetItemAsync(itemId);

            return(Ok(new BaseApiResponse <ItemServiceModel>
            {
                StatusCode = StatusCodes.Status200OK,
                Message = ResponseMessages.SuccessfulOperation,
                Body = item
            }));
        }
Esempio n. 2
0
        public async Task <IActionResult> DeleteItem(int itemId, [FromQuery] string userId, [FromQuery] string securityHash)
        {
            Guard.Argument(itemId, nameof(itemId)).GreaterThan(0);

            // Validate security hash provided in the request
            bool isValidSecurityHash = SecurityHashValidator.IsValidSecurityHash(userId, securityHash);

            // If the security hash is invalid it means it's been tempered with, so we terminate the request
            if (!isValidSecurityHash)
            {
                return(Ok(new BaseApiResponse <ItemServiceModel>
                {
                    StatusCode = StatusCodes.Status401Unauthorized,
                    Message = ResponseMessages.Unauthorized,
                }));
            }

            // Check if item exists
            ItemServiceModel item = await this._itemService.GetItemAsync(itemId);

            if (item == null)
            {
                return(Ok(new BaseApiResponse <ItemServiceModel>
                {
                    StatusCode = StatusCodes.Status404NotFound,
                    Message = ResponseMessages.NotFound,
                }));
            }

            // Delete
            bool result = await this._itemService.Delete(new DeleteItemServiceModel
            {
                ItemId = itemId,
                UserId = userId,
            });

            if (!result)
            {
                return(Problem(ResponseMessages.ServerError, null, 500));
            }

            return(Ok(new BaseApiResponse <ItemServiceModel>
            {
                StatusCode = StatusCodes.Status200OK,
                Message = ResponseMessages.SuccessfulOperation,
                Body = item
            }));
        }