コード例 #1
0
        public async Task <IActionResult> PutMessage([FromRoute] int id, [FromBody] Message message)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != message.MessageId)
            {
                return(BadRequest());
            }

            _context.Entry(message).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MessageExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #2
0
        public async Task <IActionResult> PutMessage([FromRoute] int id, [FromBody] Message message)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                Message oldMessage = await _context.Messages.FirstOrDefaultAsync(n => n.Id == id);

                if (oldMessage == null)
                {
                    return(NotFound());
                }

                if (id != message.Id)
                {
                    return(BadRequest());
                }

                oldMessage.Content      = message.Content;
                oldMessage.SeenByUserTo = false;

                _context.Entry(message).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
コード例 #3
0
 public void Update(T entity)
 {
     m_DbContext.Entry(entity).State = EntityState.Modified;
     m_DbContext.SaveChanges();
 }
        public IActionResult GetChatUsersByChatId(int id, [FromHeader] int UserID, [FromHeader] string UserRole)
        {
            var chat = _context.Chats.Find(id);

            if (chat == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound));
            }

            if (_user.GetUserById(UserID) == null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest));
            }

            _context.Entry(chat).Collection(chat => chat.ChatUsers).Load();

            var currentUserIsParticipant = true;

            if (!chat.ChatUsers.Any(chatUser => chatUser.UserId == UserID))
            {
                currentUserIsParticipant = false;
            }

            if (!currentUserIsParticipant && UserRole != "Admin")
            {
                return(StatusCode(StatusCodes.Status403Forbidden));
            }

            var chatUser = chat.ChatUsers.FirstOrDefault(chatUser => chatUser.UserId == UserID);

            if (chatUser.RequestPending)
            {
                return(StatusCode(StatusCodes.Status403Forbidden));
            }

            return(StatusCode(StatusCodes.Status200OK, new JsonResult(chat.ChatUsers)));
        }