Ejemplo n.º 1
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)
            });
        }
Ejemplo n.º 2
0
        public override async Task Listen(ListenRequest request, IServerStreamWriter <ListenResponse> responseStream, ServerCallContext context)
        {
            var userName = context.GetUserName();
            var channel  = _chatter.Listen(request.RoomId);

            while (await channel.WaitToReadAsync())
            {
                while (channel.TryRead(out var message))
                {
                    if (message.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    await responseStream.WriteAsync(new ListenResponse
                    {
                        UserName = message.UserName,
                        Message  = message.Text
                    });
                }
            }
        }
Ejemplo n.º 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)
            });
        }
Ejemplo n.º 4
0
        public override async Task <SayResponse> Say(SayRequest request, ServerCallContext context)
        {
            await _chatter.SayAsync(request.RoomId, context.GetUserName(), request.Message);

            return(new SayResponse());
        }