Ejemplo n.º 1
0
        public async Task <IActionResult> GetNoteAsync(
            [FromRoute] string noteId,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (!this.TryGetSessionState(this.HttpContext.Request.Cookies, out var state))
            {
                return(this.Unauthorized());
            }
            if (!Guid.TryParse(noteId, out var noteIdGuid))
            {
                var error = ServiceErrorResponses.NoteNotFound(noteId);
                return(this.NotFound(error));
            }

            Models.Notes.Note modelNote = null;
            try
            {
                modelNote = await this.repository.GetAsync(noteIdGuid, cancellationToken).ConfigureAwait(false);
            }
            catch (Models.Notes.NoteNotFoundExcepction)
            {
                var error = ServiceErrorResponses.NoteNotFound(noteId);
                return(this.NotFound(error));
            }
            if (!state.UserId.Equals(modelNote.UserId))
            {
                return(this.Forbid());
            }

            var clientNote = NoteConverter.Convert(modelNote);

            return(this.Ok(clientNote));
        }
Ejemplo n.º 2
0
 private async Task <bool> IsNoteBelongsToUserAsync(
     SessionState state, Guid noteId,
     CancellationToken cancellationToken)
 {
     Models.Notes.Note modelNote = null;
     try
     {
         modelNote = await this.repository.GetAsync(noteId, cancellationToken).ConfigureAwait(false);
     }
     catch (Models.Notes.NoteNotFoundExcepction)
     {
         return(false);
     }
     return(modelNote.UserId == state.UserId);
 }
Ejemplo n.º 3
0
        public async Task <IActionResult> PatchNoteAsync(
            [FromRoute] string noteId,
            [FromBody] Client.Notes.NotePatchInfo patchInfo,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (!this.TryGetSessionState(this.HttpContext.Request.Cookies, out var state))
            {
                return(this.Unauthorized());
            }
            if (patchInfo == null)
            {
                var error = ServiceErrorResponses.BodyIsMissing("NotePatchInfo");
                return(this.BadRequest(error));
            }
            if (!Guid.TryParse(noteId, out var noteIdGuid))
            {
                var error = ServiceErrorResponses.NoteNotFound(noteId);
                return(this.NotFound(error));
            }
            if (!this.IsNoteBelongsToUserAsync(state, noteIdGuid, cancellationToken).Result)
            {
                return(this.Forbid());
            }

            Models.Notes.Note modelNote = null;
            var modelPathInfo           = NotePathcInfoConverter.Convert(noteIdGuid, patchInfo);

            try
            {
                modelNote = await this.repository.PatchAsync(modelPathInfo, cancellationToken).ConfigureAwait(false);
            }
            catch (Models.Notes.NoteNotFoundExcepction)
            {
                var error = ServiceErrorResponses.NoteNotFound(noteId);
                return(this.NotFound(error));
            }

            var clientNote = NoteConverter.Convert(modelNote);

            return(this.Ok(clientNote));
        }