public async Task <dynamic> addBuddy([FromBody] BuddyDTO buddyReq)
        {
            AccountFriend  existingFriend  = db.AccountFriend.Where(af => af.AccountId == buddyReq.AccountId && af.FriendAccountId == buddyReq.BuddyAccountId).FirstOrDefault();
            AccountIgnored existingIgnored = db.AccountIgnored.Where(af => af.AccountId == buddyReq.AccountId && af.IgnoredAccountId == buddyReq.BuddyAccountId).FirstOrDefault();

            if (existingFriend != null)
            {
                return(StatusCode(403, "Buddy already exists."));
            }

            if (existingIgnored != null)
            {
                db.AccountIgnored.Attach(existingIgnored);
                db.Entry(existingIgnored).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
            }

            AccountFriend newFriend = new AccountFriend()
            {
                AccountId       = buddyReq.AccountId,
                FriendAccountId = buddyReq.BuddyAccountId,
                CreateDt        = DateTime.UtcNow
            };

            db.AccountFriend.Add(newFriend);
            db.SaveChanges();

            return(Ok("Buddy Added"));
        }
        public async Task <dynamic> removeBuddy([FromBody] BuddyDTO buddyReq)
        {
            AccountFriend existingFriend = db.AccountFriend.Where(af => af.AccountId == buddyReq.AccountId && af.FriendAccountId == buddyReq.BuddyAccountId).FirstOrDefault();

            if (existingFriend == null)
            {
                return(StatusCode(403, "Cannot remove a buddy that isn't a buddy."));
            }

            db.AccountFriend.Attach(existingFriend);
            db.Entry(existingFriend).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
            db.SaveChanges();

            return(Ok("Buddy Removed"));
        }
Beispiel #3
0
        public IEnumerable <BuddyDTO> GetBuddiesByBeacon(int id)
        {
            var userRepository  = new UserRepository();
            var buddyRepository = new BuddyRepository();
            var buddies         = buddyRepository.GetBuddiesByBeacon(id);
            var users           = new List <BuddyDTO>();

            foreach (var buddy in buddies)
            {
                var user = userRepository.GetUser(buddy.UserId);
                var dto  = new BuddyDTO
                {
                    User   = user,
                    Status = buddy.BuddyStatus,
                    ETA    = buddy.ETA
                };
                users.Add(dto);
            }
            return(users);
        }
Beispiel #4
0
        /// <summary>
        /// Remove buddy from buddy list.
        /// </summary>
        /// <param name="removeBuddy">Remove buddy parameters.</param>
        /// <returns>Success or failure.</returns>
        public async Task <bool> RemoveBuddy(BuddyDTO removeBuddy)
        {
            bool result = false;

            try
            {
                if (_settings.SimulatedMode)
                {
                    var account = await GetAccountById(removeBuddy.AccountId);

                    var buddyAccount = await GetAccountById(removeBuddy.BuddyAccountId);

                    if (account != null && buddyAccount != null)
                    {
                        var newFriends = new List <AccountRelationDTO>();
                        foreach (var friend in account.Friends)
                        {
                            if (friend.AccountId == buddyAccount.AccountId)
                            {
                                continue;
                            }

                            newFriends.Add(friend);
                        }
                        account.Friends = newFriends.ToArray();
                        result          = true;
                    }
                }
                else
                {
                    result = (await PostDbAsync($"Buddy/removeBuddy", JsonConvert.SerializeObject(removeBuddy))).IsSuccessStatusCode;
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }

            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// Add buddy to buddy list.
        /// </summary>
        /// <param name="addBuddy">Add buddy parameters.</param>
        /// <returns>Success or failure.</returns>
        public async Task <bool> AddBuddy(BuddyDTO addBuddy)
        {
            bool result = false;

            try
            {
                if (_settings.SimulatedMode)
                {
                    var account = await GetAccountById(addBuddy.AccountId);

                    var buddyAccount = await GetAccountById(addBuddy.BuddyAccountId);

                    if (account != null && buddyAccount != null)
                    {
                        var friends = account.Friends;
                        Array.Resize(ref friends, account.Friends.Length + 1);
                        friends[friends.Length - 1] = new AccountRelationDTO()
                        {
                            AccountId   = buddyAccount.AccountId,
                            AccountName = buddyAccount.AccountName
                        };
                        account.Friends = friends;
                        result          = true;
                    }
                }
                else
                {
                    result = (await PostDbAsync($"Buddy/addBuddy", JsonConvert.SerializeObject(addBuddy))).IsSuccessStatusCode;
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }

            return(result);
        }