public async Task <IActionResult> Post([FromBody] StorePutRequest request) { var response = await _storeService.Post(request); if (!response.HasError) { return(Ok(response.Data)); } return(BadRequest(response.Errors)); }
public async Task <IActionResult> Put([FromRoute] int id, double?expireIn, [FromBody] StorePutRequest request) { var response = await _storeService.Put(id, expireIn, request); if (!response.HasError && response.Data != null) { return(Ok(response.Data)); } if (!response.HasError && response.Data == null) { return(NotFound()); } return(BadRequest(response.Errors)); }
public async Task <BaseResponse <StorePostResponse> > Post(StorePutRequest request) { var response = new BaseResponse <StorePostResponse>(); if (string.IsNullOrEmpty(request.Key)) { response.Errors.Add("Key cannot be null or empty."); } if (string.IsNullOrEmpty(request.Value)) { response.Errors.Add("Value cannot be null or empty."); } if (!request.ExpirationTime.HasValue) { request.ExpirationTime = DateTime.Now.AddDays(1); } var store = new Data.Entities.Store { Key = request.Key, Value = request.Value, ExpirationTime = request.ExpirationTime }; if (await _repository.Head(store.Key, store.Value)) { response.Errors.Add("This record already exists."); } if (!response.HasError) { var entity = await _repository.Post(store); response.Data = entity.ConvertToStorePostResponse(); } return(response); }
public async Task <BaseResponse <StorePutResponse> > Put(int id, double?expireIn, StorePutRequest request) { var response = new BaseResponse <StorePutResponse>(); if (!id.IsGreaterThanZero()) { response.Errors.Add("Id must be greater than zero."); } if (string.IsNullOrEmpty(request.Key)) { response.Errors.Add("Key cannot be null or empty."); } if (string.IsNullOrEmpty(request.Value)) { response.Errors.Add("Value cannot be null or empty."); } if (await _repository.Head(request.Key, request.Value)) { response.Errors.Add("This record already exists."); } try { if (!response.HasError) { var store = await _repository.Get(id); if (store == null) { response.Errors.Add("No such record exists."); } else { store.Key = request.Key; store.Value = request.Value; if (expireIn.HasValue) { store.ExpirationTime = DateTime.Now.AddSeconds((double)expireIn); } var entity = await _repository.Update(store); response.Data = entity.ConvertToStorePutResponse(); } } } catch (Exception ex) { _logger.LogError(ex, ex.Message); response.Errors.Add("An error occurred while processing your request."); } return(response); }