Пример #1
0
        public IActionResult DeleteById(int id, [FromHeader] int UserID, [FromHeader] string UserRole)
        {
            var deletedComment = _context.Comments.Find(id);

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

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

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

            _context.Comments.Remove(deletedComment);
            var success = _context.SaveChanges();

            if (success < 1)
            {
                return(StatusCode(StatusCodes.Status400BadRequest));
            }

            this._loggerCommunication.logAction("Deleted a comment with id:" + id);

            return(StatusCode(StatusCodes.Status202Accepted, new JsonResult(deletedComment)));
        }
Пример #2
0
        public IActionResult CreateReactionType([FromBody] ReactionTypeDTO reactionType, [FromHeader] int UserId, [FromHeader] string UserRole)
        {
            if (_user.GetUserById(UserId) == null || UserRole != "Admin")
            {
                return(StatusCode(StatusCodes.Status400BadRequest));
            }

            var newReactionType = new ReactionType()
            {
                Name        = reactionType.Name,
                Description = reactionType.Description
            };

            _context.ReactionTypes.Add(newReactionType);
            var success = _context.SaveChanges();

            if (success < 1)
            {
                return(StatusCode(StatusCodes.Status400BadRequest));
            }
            return(StatusCode(StatusCodes.Status201Created, new JsonResult(newReactionType)));
        }
        public IActionResult GetChatsByUserId([FromHeader] int UserID)
        {
            var chats = _context.Chats.Include(chat => chat.ChatUsers).Where(chat => chat.ChatUsers.Any(chatUser => chatUser.UserId == UserID)).ToList();

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

            if (chats.Count == 0)
            {
                return(StatusCode(StatusCodes.Status404NotFound));
            }

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