Пример #1
0
        public async Task <IActionResult> AddNote([FromBody] string?title, [FromBody] string?content)
        {
            var userName = this.HttpContext.GetUserName();

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

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

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

            if (title is null || content is null)
            {
                return(BadRequest());
            }

            var input = new NotesRepository.NoteInput(title, content);

            var note = await _notes.AddNote(input, creator.Value, _cancellation);

            return(new RestResult(NoteDTO.FromNote(note)));
        }
Пример #2
0
        public async ValueTask <AddNoteResponse> Handle(AddNoteRequest request, ServerCallContext ctx)
        {
            var userName = ctx.GetUserName();
            var creator  = await _authors.GetByUserName(userName, ctx.CancellationToken);

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

            if (string.IsNullOrEmpty(request.Title))
            {
                throw new ArgumentNullRpcException("title");
            }

            if (string.IsNullOrEmpty(request.Content))
            {
                throw new ArgumentNullRpcException("content");
            }

            var input   = new NotesRepository.NoteInput(request.Title, request.Content);
            var newNote = await _notes.AddNote(input, creator.Value, ctx.CancellationToken);

            return(new AddNoteResponse {
                CreatedNote = ModelMapper.MapNote(newNote)
            });
        }