コード例 #1
0
        public void Post(HttpRequestMessage request, long toAccount)
        {
            long senderAccountId;

            if (!_authenticationManager.TryAuthenticateForWriteOperation(request.GetToken(), out senderAccountId))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }

            if (!UsersInTheSameRoom(toAccount, senderAccountId))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
            }


            var messageStr = request.GetHeader("textMessage");

            if (string.IsNullOrWhiteSpace(messageStr))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            var message = new UserMessage(new LazyAccount(toAccount), new LazyAccount(senderAccountId), _nowProvider.Now,
                                          messageStr);

            try
            {
                _userMessageRepository.Add(message);
            }
            catch (Exception ex)
            {
                Log.Warn("unable to send message", ex);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }
コード例 #2
0
        public IActionResult Post(UserMessage message)
        {
            var currentUser = GetCurrentUserProfile();

            message.SenderId       = currentUser.Id;
            message.CreateDateTime = DateTime.Now;

            try
            {
                _userMessageRepository.Add(message);
                return(CreatedAtAction("Get", new { id = message.Id }, message));
            }
            catch
            {
                return(StatusCode(500));
            }
        }