public async Task <IActionResult> Delete(int id) { if (id <= 0) { return(BadRequest($"Parameter '{nameof(id)}' should be greater than zero.")); } try { IInt32Identity entity = await entityRepository.GetById(id); if (entity == null) { return(BadRequest($"Item with Id = '{id}' does not exist.")); } T toBeDeleted = new T() { Id = id }; await entityRepository.Delete(toBeDeleted); return(Ok()); } catch (Exception ex) { logger?.LogError(ex, ex.Message); return(BadRequest(ex.Message)); } }
public async Task <IActionResult> Update(int id, T newModel) { try { IInt32Identity entity = await entityRepository.GetById(id); if (entity == null) { return(BadRequest($"Item with Id = '{id}' does not exist.")); } bool isUpdateSuccessful = await entityRepository.Update(id, newModel); if (isUpdateSuccessful) { return(Ok()); } else { return(StatusCode(StatusCodes.Status204NoContent, "No entities updated.")); } } catch (Exception ex) { logger?.LogError(ex, ex.Message); return(BadRequest(ex.Message)); } }
public async Task <IActionResult> GetRandom() { try { IInt32Identity entity = await entityRepository.GetRandom(); return(Ok(entity)); } catch (Exception ex) { logger?.LogError(ex, ex.Message); return(BadRequest(ex.Message)); } }
public async Task <IActionResult> GetById(int id) { if (id <= 0) { return(BadRequest($"Parameter '{nameof(id)}' should be greater than zero.")); } try { IInt32Identity entity = await entityRepository.GetById(id); return(Ok(entity)); } catch (Exception ex) { logger?.LogError(ex, ex.Message); return(BadRequest(ex.Message)); } }