コード例 #1
0
        public IActionResult PostComment(int id, [FromBody] Comment comment)
        {
            comment.MessageId = id;

            var profileId = comment.ProfileId;

            var profile       = _context.Profile.Find(profileId);
            var messageHelper = new MessageHelper();
            var helper        = messageHelper.IsProfileActive(profile);

            if (helper == false)
            {
                return(BadRequest("User is not authorized to post"));
            }

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

            if (comment.Message.Length > 200)
            {
                return(BadRequest("Comment must be 200 characters or less"));
            }

            _context.Comment.Add(comment);
            _context.SaveChanges();

            var commentPresentation = new CommentPresentation(comment.Id, comment.Message, comment.Profile);

            return(Ok(commentPresentation));
        }
コード例 #2
0
        public IActionResult CreateProfile([FromBody] ProfileModels profile)
        {
            var p = new Profile();
            var usernameExists = _context.Profile.Where(x => x.UserName == profile.UserName).FirstOrDefault() != null;

            if (profile.FullName == null)
            {
                return(BadRequest("No Full Name given"));
            }
            else if (usernameExists)
            {
                return(BadRequest("Username already exists."));
            }
            else if (profile.Password == null)
            {
                return(BadRequest("No password given"));
            }
            else if (profile != null)
            {
                profile.Status = ProfileState.Active;
            }

            p.FullName = profile.FullName;
            p.UserName = profile.UserName;
            p.Password = profile.Password;
            p.Status   = profile.Status;

            _context.Profile.Add(p);

            _context.SaveChanges();

            profile.Id = p.Id;

            if (profile.Picture != null)
            {
                var image = new ImageHandler(connectionString: _config.GetConnectionString("TwittDatabase"));

                image.StoreImageProfile(profile);
            }

            return(Ok($"{profile.UserName} was created."));
        }
コード例 #3
0
        public IActionResult Reaction(int postId, [FromBody] ReactionModels reaction)
        {
            reaction.MessageID = postId;
            var prevReaction = _context.Reaction.Where(x => x.Profile == reaction.ProfileID && x.Message == reaction.MessageID).FirstOrDefault();
            var stringValue  = prevReaction == null ? "" : Reactions.ConvertToString(prevReaction.LikeOrDislike);

            if (reaction.ProfileID != 0 && reaction.MessageID != 0)
            {
                var react         = new Reaction();
                var userReactions = _context.Reaction
                                    .Where(r => r.Profile == reaction.ProfileID && r.Message == reaction.MessageID)
                                    .Count();

                react.Message = reaction.MessageID;
                react.Profile = reaction.ProfileID;

                if (reaction.LikeOrDislike == "Like")
                {
                    react.LikeOrDislike = true;
                }
                else if (reaction.LikeOrDislike == "DisLike")
                {
                    react.LikeOrDislike = false;
                }

                if (userReactions < 1)
                {
                    _context.Reaction.Add(react);
                }
                else if (reaction.LikeOrDislike == stringValue)
                {
                    _context.Remove(prevReaction);
                }
                else if (reaction.LikeOrDislike != stringValue)
                {
                    _context.Reaction.Remove(prevReaction);
                    _context.Add(react);
                }

                else
                {
                    return(BadRequest("Cannont give same reaction twice."));
                }

                _context.SaveChanges();
                var likes    = _context.Reaction.Where(l => l.LikeOrDislike == Reactions.Like && l.Message == reaction.MessageID).Count();
                var dislikes = _context.Reaction.Where(l => l.LikeOrDislike == Reactions.DisLike && l.Message == reaction.MessageID).Count();
                return(Ok(new { likes, dislikes }));
            }
            return(BadRequest("Data is missing from the request."));
        }
コード例 #4
0
        public IActionResult PostMessage(int profileId, [FromBody] MessageModels post)
        {
            var profile       = _context.Profile.Find(profileId);
            var messageHelper = new MessageHelper();
            var helper        = messageHelper.IsProfileActive(profile);

            if (helper == false)
            {
                return(BadRequest("User is not authorized to post"));
            }

            var p = new Message();

            post.ProfileId = profileId;

            if (post.ProfileId != profileId)
            {
                return(BadRequest());
            }

            if (post.Message.Length > 200)
            {
                return(BadRequest("Message must be 200 characters or less"));
            }

            p.Text      = post.Message;
            p.ProfileId = post.ProfileId;

            _context.Message.Add(p);

            _context.SaveChanges();

            post.Id = p.Id;

            if (post.Picture != null)
            {
                var image = new ImageHandler(connectionString: _config.GetConnectionString("TwittDatabase"));

                image.StoreImagePost(post);
            }

            return(Ok(post));
        }