Esempio n. 1
0
        // POST: api/friend
        public async Task <IActionResult> Create([FromBody] AddFriendModel friend)
        {
            try
            {
                var userId = _userManager.GetUserId(User);
                if (userId == null)
                {
                    _logger.LogError($"Error inside FriendController Create action: UserId not found");
                    return(Unauthorized("You must be logged in to add a friend"));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError($"Error inside FriendController Create action: AddFriendModel not valid");
                    return(BadRequest("Invalid model object"));
                }

                var newFriend = _mapper.Map <Friend>(friend);
                newFriend.UserId = userId; //userId isn't added from the client side model

                _repoWrapper.Friends.Create(newFriend);
                _repoWrapper.UnitOfWorkComplete();

                return(CreatedAtRoute("GetFriend", new { id = newFriend.Id }, newFriend));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error inside FriendController Create action: {ex.Message}");
                return(StatusCode(500, "Internal Sever Error"));
            }
        }
Esempio n. 2
0
 public async Task <object> RemoveFriend(AddFriendModel unFriends)
 {
     try
     {
         return(await _userService.RemoveFriend(unFriends));
     }
     catch (Exception e)
     {
         return(new { Message = $"Friend unsuccessfully removed! [{e.Message}]" });
     }
 }
Esempio n. 3
0
 public async Task <object> AddFriend(AddFriendModel newFriends)
 {
     try
     {
         return(await _userService.AddFriend(newFriends));
     }
     catch (Exception e)
     {
         return(BadRequest(new { Message = $"Error while adding new friend. [{e.Message}]" }));
     }
 }
        public async Task <ActionResult <int> > Create(AddFriendModel input)
        {
            var friend = new Friend
            {
                FriendUserId = input.FriendUserId,
                UserId       = this.currentUser.UserId
            };

            await this.friends.Save(friend);

            // todo send message

            return(friend.Id);
        }
Esempio n. 5
0
        public void AddFriend(AddFriendModel addModel)
        {
            var exists = _userRepository.GetUserByPhoneNumber(addModel.TargetPhoneNumber);

            if (exists == null)
            {
                throw new Exception("User not found!");
            }
            var friendsList = _friendsRepository.GetAll(addModel.PhoneNumber);

            if (friendsList.Contains(addModel.TargetPhoneNumber))
            {
                throw new Exception("Friend already in the list!");
            }
            _friendsRepository.AddFriend(addModel.PhoneNumber, addModel.TargetPhoneNumber);
        }
Esempio n. 6
0
 public IActionResult AddFriend([FromBody] AddFriendModel add)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     try
     {
         _friendsService.AddFriend(add);
         return(Ok());
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Esempio n. 7
0
        public async Task <object> RemoveFriend(AddFriendModel unFriends)
        {
            User user = await _userManager.FindByEmailAsync(unFriends.UsersEmail);

            User friend = await _userManager.FindByEmailAsync(unFriends.FriendsEmail);

            Friend friendToRemove = _context
                                    .Friend
                                    .ToList()
                                    .Find(
                x => x.User1Email == user.Email && x.User2Email == friend.Email ||
                x.User1Email == friend.Email && x.User2Email == user.Email
                );

            _context.Friend.Remove(friendToRemove);

            await _context.SaveChangesAsync();

            return(new { Message = "Friend successfully removed!" });
        }
Esempio n. 8
0
        public ResponseMessage AddFriend([FromBody] AddFriendModel player)
        {
            if (!ModelState.IsValid)
            {
                return(new ResponseMessage("fail!!!"));
            }

            if (player == null)
            {
                return(new ResponseMessage("dữ liệu trống!!!"));
            }
            //khi đã là bạn rồi thì không thêm đc nữa
            foreach (player_listfriend item in db.player_listfriend)
            {
                if (player.friend_id == item.friend_id)
                {
                    return(new ResponseMessage(player.player_id + " đã là bạn với " + player.friend_id));
                }
            }
            //chỉ thêm bạn khi isban = false
            if (player.isban == false)
            {
                player_listfriend player_ = new player_listfriend();
                player_.player_id = player.player_id;
                player_.friend_id = player.friend_id;
                db.player_listfriend.Add(player_);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    return(new ResponseMessage("fail"));
                }
                return(new ResponseMessage(player.player_id + " là bạn của " + player.friend_id));
            }
            else
            {
                return(new ResponseMessage("Bạn đã từ chối lời mời kết bạn của " + player.friend_id));
            }
        }
Esempio n. 9
0
        public GenericOperationResult <IActionResult> AddFriend(AddFriendModel addFriend)
        {
            var result = new GenericOperationResult <IActionResult>();

            if (addFriend.UserName != "" || addFriend.UserName != null)
            {
                try
                {
                    return(userService.AddFriend(addFriend.UserName));
                }
                catch (BusinessException ex)
                {
                    Logger.Error(ex, "error occured during adding friend");
                    result.Data = new BadRequestResult();
                    result.Messages.Add("Bad data can;t add friend try again ");
                    result.Status = OperationResultStatusEnum.Failed;
                    return(result);
                }
            }
            result.Data   = new BadRequestResult();
            result.Status = OperationResultStatusEnum.Failed;
            result.Messages.Add("userName can't be empty");
            return(result);
        }
Esempio n. 10
0
        public async Task <object> AddFriend(AddFriendModel newFriends)
        {
            var user1 = await _userManager.FindByEmailAsync(newFriends.UsersEmail);

            var user2 = await _userManager.FindByEmailAsync(newFriends.FriendsEmail);

            var friends = _context.Friend.ToList().Find(x => x.User1Email == user1.Email && x.User2Email == user2.Email);

            if (friends != null)
            {
                return(new { Message = "You are already friends!" });
            }

            _context.Friend.Add(
                new Friend()
            {
                User1Email = user1.Email,
                User2Email = user2.Email
            });

            await _context.SaveChangesAsync();

            return(new { Message = "Friend successfully added!" });
        }
        public IHttpActionResult AddFriend(AddFriendModel addFriend)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string token = Request.Headers.GetValues("token").First().ToString();


            User user = _context.Users.FirstOrDefault(u => u.Token == token);

            if (user == null)
            {
                ModelState.AddModelError("Error", Messages.UsrNotFound);
                return(BadRequest(ModelState));
            }

            User friend = _context.Users.FirstOrDefault(f => f.Email == addFriend.Email);

            if (friend == null || friend.Email == user.Email)
            {
                return(BadRequest(Messages.UsrNotFound));
            }
            string Message;

            Friend checkFriend = _context.Friends.FirstOrDefault(f => (f.SenderId == user.Id && f.ReceiverId == friend.Id) || (f.SenderId == friend.Id && f.ReceiverId == user.Id));

            if (checkFriend != null)
            {
                if (checkFriend.isFriend == false)
                {
                    Message = Messages.AlrdReqFriend;
                }
                else
                {
                    Message = Messages.AlrdFriends;
                }

                return(Ok(new { Message }));
            }

            Friend addFriendInDb = new Friend()
            {
                SenderId          = user.Id,
                ReceiverId        = friend.Id,
                isFriend          = false,
                isSenderBlocked   = false,
                isReceiverBlocked = false,
                CreateAt          = DateTime.Now
            };

            _context.Friends.Add(addFriendInDb);

            _context.SaveChanges();


            Message = Messages.UsrReq;


            return(Ok(new { Message }));
        }
Esempio n. 12
0
 public static void MapAddFriendToFriend(this Friend newFriend, AddFriendModel friendToAdd)
 {
     newFriend.Name       = friendToAdd.Name;
     newFriend.LocationId = (friendToAdd.LocationId != null)? (int)friendToAdd.LocationId : newFriend.LocationId;
 }