示例#1
0
        public async Task <IActionResult> LikeMessage(int messageId)
        {
            var message = await _context.Messages.Where(m => m.Id == messageId).FirstOrDefaultAsync();

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

            var newMsgLike = new MessageLike()
            {
                MessageId = messageId, UserId = 3
            };


            var msgLike = _context.MessageLikes.FirstOrDefault(ml => ml.MessageId == newMsgLike.MessageId && ml.UserId == 3);

            if (msgLike == null)
            {
                await _context.MessageLikes.AddAsync(newMsgLike);
            }
            else
            {
                _context.MessageLikes.Remove(msgLike);
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }
示例#2
0
        public async Task <IActionResult> PutUser(int id, User user)
        {
            if (id != user.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }