예제 #1
0
        public async Task <IActionResult> AddKeywordToNote(int id, [FromForm] string keyword)
        {
            if (keyword is null)
            {
                return(BadRequest());
            }

            var userName = this.HttpContext.GetUserName();

            if (userName is null)
            {
                return(Unauthorized());
            }

            var editor = await _authors.GetByUserName(userName, _cancellation);

            if (!editor.HasValue)
            {
                return(Unauthorized());
            }

            var exists = await _notes.AddKeywordToNote(id, keyword, editor.Value, _cancellation);

            if (!exists)
            {
                return(NotFound());
            }

            var note = await _notes.GetById(id, _cancellation);

            return(new RestResult(note.Value.Keywords.Select(_ => _.Value)));
        }
예제 #2
0
        public async Task <Keyword> CreateAsync(Keyword entity)
        {
            var context  = _httpContextAccessor.HttpContext;
            var userName = context.GetUserName();

            if (userName is null)
            {
                throw new JsonApiException(new Error(HttpStatusCode.Unauthorized));
            }

            var editor = await _authors.GetByUserName(userName);

            if (!editor.HasValue)
            {
                throw new JsonApiException(new Error(HttpStatusCode.Unauthorized));
            }

            var noteId = int.Parse(context.GetRouteValue("noteId") as string);

            var found = await _notes.AddKeywordToNote(noteId, entity.Value, editor.Value);

            if (!found)
            {
                throw new JsonApiException(new Error(HttpStatusCode.NotFound));
            }

            return(entity);
        }
예제 #3
0
        public async ValueTask <AddKeywordResponse> Handle(AddKeywordRequest request, ServerCallContext ctx)
        {
            var userName = ctx.GetUserName();
            var editor   = await _authors.GetByUserName(userName, ctx.CancellationToken);

            if (!editor.HasValue)
            {
                throw new UserNotFoundRpcException(userName);
            }

            if (request.NoteId is null || request.NoteId.Value == default)
            {
                throw new ArgumentNullRpcException("note_id");
            }

            if (string.IsNullOrEmpty(request.NewKeyword))
            {
                throw new ArgumentNullRpcException("new_keyword");
            }

            var id      = (int)request.NoteId.Value;
            var keyword = request.NewKeyword;
            var found   = await _notes.AddKeywordToNote(id, keyword, editor.Value, ctx.CancellationToken);

            if (!found)
            {
                throw new NotFoundRpcException("Note", id);
            }

            var updatedNote = await _notes.GetById(id, ctx.CancellationToken);

            return(new AddKeywordResponse()
            {
                UpdatedNote = ModelMapper.MapNote(updatedNote.Value)
            });
        }
예제 #4
0
        public NotesMutation(NotesRepository notes, AuthorsRepository authors)
        {
            FieldAsync <NonNullGraphType <NoteGraphType> >(
                name: "addNote",
                description: "Add a new note.",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <NoteInputType> >()
            {
                Name        = "note",
                Description = "The new note to be added."
            }),
                resolve: async ctx =>
            {
                var userName = ctx.RequireUserName();
                var creator  = await authors.GetByUserName(userName, ctx.CancellationToken);

                if (!creator.HasValue)
                {
                    return(Errors.InvalidUsernameError <object>());
                }

                var newNote = ctx.GetRequiredArgument <NotesRepository.NoteInput>("note");

                return(await notes.AddNote(newNote, creator.Value, ctx.CancellationToken));
            });

            FieldAsync <NoteGraphType>(
                name: "addKeyword",
                description: "Add a new keyword to an existing note with the given id.",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> >()
            {
                Name        = "noteId",
                Description = "The id of the note the keyword should be added to."
            },
                    new QueryArgument <NonNullGraphType <KeywordGraphType> >()
            {
                Name        = "keyword",
                Description = "The keyword to add to the note."
            }),
                resolve: async ctx =>
            {
                var prefixedId = ctx.GetRequiredArgument <string>("noteId");
                var id         = IdParser.ParsePrefixedId(prefixedId, NoteGraphType.IdPrefix);

                var keyword = ctx.GetRequiredArgument <Keyword>("keyword");

                var userName = ctx.RequireUserName();
                var editor   = await authors.GetByUserName(userName, ctx.CancellationToken);

                if (!editor.HasValue)
                {
                    return(Errors.InvalidUsernameError <object>());
                }

                var found = await notes.AddKeywordToNote(id, keyword, editor.Value, ctx.CancellationToken);

                if (!found)
                {
                    return(Errors.NotFound <object>("Note", prefixedId));
                }

                return(await notes.GetById(id, ctx.CancellationToken));
            });

            FieldAsync <BooleanGraphType>(
                name: "deleteNote",
                description: "Deletes the note with the given id, if present.",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> >()
            {
                Name        = "noteId",
                Description = "The id of the note that should be deleted."
            }),
                resolve: async ctx =>
            {
                var prefixedId = ctx.GetRequiredArgument <string>("noteId");
                var id         = IdParser.ParsePrefixedId(prefixedId, NoteGraphType.IdPrefix);

                var found = await notes.Remove(id, ctx.CancellationToken);

                if (!found)
                {
                    return(Errors.NotFound <bool>("Note", prefixedId));
                }

                return(true);
            });

            FieldAsync <NonNullGraphType <AuthorGraphType> >(
                name: "signup",
                description: "Create a new author.",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <AuthorInputType> >()
            {
                Name        = "author",
                Description = "The new author to be added."
            }),
                resolve: async ctx =>
            {
                var newAuthor = ctx.GetRequiredArgument <AuthorsRepository.AuthorInput>("author");
                return(await authors.AddAuthor(newAuthor, ctx.CancellationToken));
            });
        }