public async Task <IActionResult> GetTodoItemAsync([FromRoute] string todoId, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!Guid.TryParse(todoId, out var modelTodoItemId)) { var error = ServiceErrorResponses.TodoNotFound(todoId); return(NotFound(error)); } if (!HttpContext.Items.TryGetValue("userId", out var userId) || !Guid.TryParse(userId.ToString(), out var userGuid)) { var error = ServiceErrorResponses.UnAuthorized(); return(BadRequest(error)); } Model.TodoItem modelTodoItem = null; try { modelTodoItem = await repository.GetAsync(modelTodoItemId, userGuid, cancellationToken).ConfigureAwait(false); } catch (Model.TodoNotFoundException) { var error = ServiceErrorResponses.TodoNotFound(todoId); return(NotFound(error)); } var clientTodoItem = ModelsConverters.TodoItems.TodoItemConverter.Convert(modelTodoItem); return(Ok(clientTodoItem)); }
public async Task <IActionResult> DeleteTodoItemAsync([FromRoute] string todoId, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (!Guid.TryParse(todoId, out var todoGuid)) { var error = ServiceErrorResponses.TodoNotFound(todoId); return(NotFound(error)); } if (!HttpContext.Items.TryGetValue("userId", out var userId) || !Guid.TryParse(userId.ToString(), out var userGuid)) { var error = ServiceErrorResponses.UnAuthorized(); return(BadRequest(error)); } try { await repository.RemoveAsync(todoGuid, userGuid, cancellationToken).ConfigureAwait(false); } catch (Model.TodoNotFoundException) { var error = ServiceErrorResponses.TodoNotFound(todoId); return(NotFound(error)); } return(NoContent()); }
public async Task <IActionResult> GetTodoAsync([FromRoute] string recordId, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); Todo record; var nameIdentifier = this.HttpContext.User.Claims .FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier); var user = await _userRepository.GetAsync(nameIdentifier.Value, cancellationToken).ConfigureAwait(false); if (!Guid.TryParse(recordId, out var modelTodoId)) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(this.NotFound(error)); } if (!await IsSameUser(modelTodoId, cancellationToken)) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(NotFound(error)); } try { record = await _todoService.GetAsync(modelTodoId, cancellationToken).ConfigureAwait(false); } catch (RecordNotFoundException e) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(NotFound(error)); } var clientRecord = TodoConverter.Convert(record); return(this.Ok(clientRecord)); }
public async Task <IActionResult> DeleteNoteAsync([FromRoute] string recordId, CancellationToken cancellationToken) { if (!Guid.TryParse(recordId, out var recordIdGuid)) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(this.NotFound(error)); } if (!await IsSameUser(recordIdGuid, cancellationToken)) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(NotFound(error)); } try { await this._todoService.RemoveAsync(recordIdGuid, cancellationToken).ConfigureAwait(false); } catch (RecordNotFoundException) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(this.NotFound(error)); } return(this.NoContent()); }
public async Task <IActionResult> PatchNoteAsync([FromRoute] string recordId, [FromBody] ClientModels.Todos.TodoPatchInfo patchInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (patchInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("RecordPatchInfo"); return(this.BadRequest(error)); } if (!Guid.TryParse(recordId, out var recordIdGuid)) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(this.NotFound(error)); } if (!await IsSameUser(recordIdGuid, cancellationToken)) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(NotFound(error)); } var modelPatchInfo = TodoPathcInfoConverter.Convert(recordIdGuid, patchInfo); Models.Todos.Todo modelRecord; try { modelRecord = await this._todoService.PatchAsync(modelPatchInfo, cancellationToken).ConfigureAwait(false); } catch (RecordNotFoundException) { var error = ServiceErrorResponses.TodoNotFound(recordId); return(this.NotFound(error)); } var clientNote = TodoConverter.Convert(modelRecord); return(this.Ok(clientNote)); }