示例#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));
        }
示例#2
0
        public async Task <IActionResult> DeleteNoteAsync(
            [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));
            }
            if (!this.IsNoteBelongsToUserAsync(state, noteIdGuid, cancellationToken).Result)
            {
                return(this.Forbid());
            }

            try
            {
                await this.repository.RemoveAsync(noteIdGuid, cancellationToken).ConfigureAwait(false);
            }
            catch (Models.Notes.NoteNotFoundExcepction)
            {
                var error = ServiceErrorResponses.NoteNotFound(noteId);
                return(this.NotFound(error));
            }
            return(this.NoContent());
        }
        public async Task <IActionResult> GetNoteAsync([FromRoute] string noteId, CancellationToken cancelltionToken)
        {
            cancelltionToken.ThrowIfCancellationRequested();

            if (!Guid.TryParse(noteId, out var modelNoteId))
            {
                var error = ServiceErrorResponses.NoteNotFound(noteId);
                return(this.NotFound(error));
            }

            Model.Notes.Note modelNote = null;

            try
            {
                modelNote = await this.repository.GetAsync(modelNoteId, cancelltionToken).ConfigureAwait(false);
            }
            catch (Model.Notes.NoteNotFoundExcepction)
            {
                var error = ServiceErrorResponses.NoteNotFound(noteId);
                return(this.NotFound(error));
            }

            var clientNote = NoteConverter.Convert(modelNote);

            return(this.Ok(clientNote));
        }
        public async Task <IActionResult> PatchNoteAsync([FromRoute] string noteId, [FromBody] Client.Notes.NotePatchInfo patchInfo, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            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));
            }

            var modelPathInfo = NotePathcInfoConverter.Convert(noteIdGuid, patchInfo);

            Model.Notes.Note modelNote = null;

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

            var clientNote = NoteConverter.Convert(modelNote);

            return(this.Ok(clientNote));
        }
示例#5
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));
        }
        public async Task <IActionResult> DeleteNoteAsync([FromRoute] string noteId, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (!Guid.TryParse(noteId, out var noteIdGuid))
            {
                var error = ServiceErrorResponses.NoteNotFound(noteId);
                return(this.NotFound(error));
            }

            try
            {
                await this.repository.RemoveAsync(noteIdGuid, cancellationToken).ConfigureAwait(false);
            }
            catch (Model.Notes.NoteNotFoundExcepction)
            {
                var error = ServiceErrorResponses.NoteNotFound(noteId);
                return(this.NotFound(error));
            }

            return(this.NoContent());
        }