public async Task <IActionResult> Create([FromBody] RelationshipRequest request)
        {
            try
            {
                var relationship = await _relationshipApp.Create(request);

                var notification = new Notification()
                {
                    CreatedAt = DateTime.Now,
                    FromId    = request.FromId,
                    ToId      = request.ToId,
                    Type      = "relationship"
                };

                var notificationResponse = await _notificationApp.CreateNotification(notification);

                try
                {
                    await _notificationApp.SendNotification(notificationResponse);
                }
                catch (Exception)
                {
                }
                return(Ok(relationship));
            }
            catch (Exception e)
            {
                return(BadRequest(new
                {
                    Message = e.Message
                }));
            }
        }
        public async Task <IActionResult> LikeImage([FromForm] LikeImageRequest request)
        {
            var userInfo = _sessionService.GetDataFromToken();

            if (request.UserId != userInfo.UserId)
            {
                return(StatusCode(401, new
                {
                    Message = "userId is not correct"
                }));
            }
            var image = await _imageApplication.GetImageById(request.ImageId);

            if (image == null)
            {
                return(BadRequest(new
                {
                    Message = "Image id is not correct"
                }));
            }

            if (!await _userApplication.IsExist(request.UserId))
            {
                return(BadRequest(new
                {
                    Message = "User id is not correct"
                }));
            }

            var message = await _imageApplication.LikeImage(request);

            if (message == "Liked")
            {
                var nt = new Notification()
                {
                    CreatedAt = DateTime.Now,
                    FromId    = userInfo.UserId,
                    ToId      = image.UserId,
                    Type      = "likeImage"
                };
                var noticeRes = await _notificationApp.CreateNotification(nt);

                if (noticeRes != null)
                {
                    await _notificationApp.SendNotification(noticeRes);
                }
            }
            return(Ok(new
            {
                Message = message
            }));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Follow([FromForm] Guid userId)
        {
            var sessionUser = _sessionService.GetDataFromToken();

            if (sessionUser == null)
            {
                return(BadRequest(new
                {
                    Message = "Can not read token"
                }));
            }

            //if (sessionUser.UserId == userId)
            //{
            //    return BadRequest(new
            //    {
            //        Message = "Can not follow yourself"
            //    });
            //}

            var followed = await _context.Follows
                           .Where(x => x.FromUserId == sessionUser.UserId && x.ToUserId == userId)
                           .FirstOrDefaultAsync();

            var user = await _userApplication.GetById(sessionUser.UserId);

            var message = "";

            if (followed == null)
            {
                var follow = new Follow()
                {
                    Content    = "",
                    FromUserId = sessionUser.UserId,
                    ToUserId   = userId
                };
                user.NumberOfFiends++;
                user = await _userApplication.UpdateUser(user, false);

                _context.Follows.Add(follow);
                message = "Followed";

                var nt = new Notification()
                {
                    CreatedAt = DateTime.Now,
                    FromId    = sessionUser.UserId,
                    ToId      = userId,
                    Type      = "follow"
                };
                var noticeRes = await _notificationApp.CreateNotification(nt);

                if (noticeRes != null)
                {
                    await _notificationApp.SendNotification(noticeRes);
                }
            }
            else
            {
                user.NumberOfFiends--;
                user = await _userApplication.UpdateUser(user, false);

                _context.Follows.Remove(followed);
                message = "Unfollowed";
            }

            try
            {
                await _context.SaveChangesAsync();

                await _userApplication.UpdateSimilarityScores(sessionUser.UserId);
            }
            catch (Exception e)
            {
                return(StatusCode(501, new
                {
                    Message = e.Message
                }));
            }
            return(Ok(new
            {
                Message = message
            }));
        }