コード例 #1
0
        //Admin adds a housework assign
        public async Task AddHouseworkAsync(UserModel user, HouseworkModel housework, int friendId)
        {
            if (user.Position != (int)UserPosition.Admin)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Authorization Error", "You are not authorized for housework assignment");
                errors.Throw();
            }

            if (housework.Day < 1 || housework.Day > 31)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Housework Day Error", "You can assign a day only  between 1-31");
                errors.Throw();
            }

            user = await _userRepository.GetByIdAsync(user.Id, true);

            HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

            UserModel friend = await _userRepository.GetByIdAsync(friendId, true);

            if ((friend == null) || (friend.Home != user.Home))
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Friendship Not Found", "Friendship not found for assignment");
                errors.Throw();
            }

            housework.User = friend;
            housework.Home = home;

            _houseworkRepository.Insert(housework);

            //Sends fcm to assigned friend
            FCMModel fcmFriend = new FCMModel(friend.DeviceId, new Dictionary <string, object>());

            fcmFriend.notification.Add("title", "Yeni Ev İşi");
            fcmFriend.notification.Add("body", String.Format("Ayın {0}. günü {1} yapmanız gerekmektedir.", housework.Day, housework.Work));

            await _fcmService.SendFCMAsync(fcmFriend);

            //Sends fcm to all friends
            foreach (var f in home.Users)
            {
                FCMModel fcm = new FCMModel(f.DeviceId, type: "AddHousework");
                fcm.data.Add("Housework", housework);
                fcm.data.Add("FriendId", friendId);

                await _fcmService.SendFCMAsync(fcm);
            }
        }
コード例 #2
0
        //Sends notification to all friends for shopping
        public async Task SendNotification(UserModel user)
        {
            if (user.Position == (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Home Not Exist", "User is not member of a home");
                errors.Throw();
            }

            user = await _userRepository.GetByIdAsync(user.Id, true);

            HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

            foreach (var f in home.Users)
            {
                if (f.Id == user.Id)
                {
                    continue;
                }

                FCMModel fcm = new FCMModel(f.DeviceId, new Dictionary <string, object>());
                fcm.notification.Add("title", "Alışveriş Talebi");
                fcm.notification.Add("body", "Alışveriş listesindeki ürünlerin alınması isteniyor.");
                await _fcmService.SendFCMAsync(fcm);
            }
        }
コード例 #3
0
        //User adds note
        public async Task AddNoteAsync(UserModel user, NotepadModel note)
        {
            if (user.Position == (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Home Not Exist", "User is not member of a home");
                errors.Throw();
            }

            user = await _userRepository.GetByIdAsync(user.Id, true);

            HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

            note.Home = home;

            await _notepadRepository.InsertAsync(note);

            foreach (var friend in home.Users)
            {
                FCMModel fcm = new FCMModel(friend.DeviceId, type: "NotepadAdd");
                fcm.data.Add("NewNote", note);
                await _fcmService.SendFCMAsync(fcm);
            }
        }
コード例 #4
0
        //User adds menu
        public async Task AddMenuAsync(UserModel user, MenuModel menu, List <int> mealIds)
        {
            if (user.Position == (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Home Not Exist", "User is not member of a home");
                errors.Throw();
            }

            if (menu.Date.Kind != DateTimeKind.Utc)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Date Not Valid", "Date is not valid, please convert to UTC");
                errors.Throw();
            }

            menu.Date = menu.Date.AddHours(-menu.Date.Hour);
            menu.Date = menu.Date.AddMinutes(-menu.Date.Minute);
            menu.Date = menu.Date.AddSeconds(-menu.Date.Second);
            menu.Date = menu.Date.AddMilliseconds(-menu.Date.Millisecond);

            user = await _userRepository.GetByIdAsync(user.Id, true);

            HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

            MenuModel tmp = await _menuRepository.GetHomeMenuByDateAsync(home.Id, menu.Date);

            if (tmp != null)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Date Not Valid", "Date is not valid, that day has already menu");
                errors.Throw();
            }

            mealIds = mealIds.Distinct().ToList();

            if (mealIds.Count == 0)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Menu Has Not Meal", "There is no meals for this menu");
                errors.Throw();
            }

            menu.Home = home;

            //Finds meals that are not related home
            foreach (var mealId in mealIds)
            {
                MealModel meal = await _mealRepository.GetHomeMealByIdAsync(mealId, true);

                if ((meal == null) || (meal.Home.Id != home.Id))
                {
                    CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                    errors.AddError("Meal Not Related Home", "This meal is not related with user home");
                    errors.Throw();
                }
            }

            _menuRepository.Insert(menu);

            //Inserts menu meal to database
            foreach (var mealId in mealIds)
            {
                MealModel meal = await _mealRepository.GetHomeMealByIdAsync(mealId, true);

                MenuMealModel menuMeal = new MenuMealModel(menu, meal);

                _menuMealRepository.Insert(menuMeal);
            }

            //Sends fcm to all users
            foreach (var friend in home.Users)
            {
                FCMModel fcm = new FCMModel(friend.DeviceId, type: "AddMenu");
                fcm.data.Add("Menu", menu);
                fcm.data.Add("MealIds", mealIds);

                await _fcmService.SendFCMAsync(fcm);
            }
        }
コード例 #5
0
        //Admin accepts or rejects user's request
        public async Task JoinHomeAcceptAsync(UserModel user, int requesterId, bool isAccepted)
        {
            Task <UserModel>        getAdmin      = _userRepository.GetByIdAsync(user.Id, true);
            Task <InformationModel> firstNameInfo = _informationRepository.GetInformationByInformationNameAsync("FirstName");
            Task <InformationModel> lastNameInfo  = _informationRepository.GetInformationByInformationNameAsync("LastName");

            if (user.Position != (int)UserPosition.Admin)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Authorisation Constraint", "You are not authorized for this request, you must be administrator of home");
                errors.Throw();
            }

            UserModel requester = await _userRepository.GetByIdAsync(requesterId);

            if (requester == null)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Invalid User Id", "User not exist");
                errors.Throw();
            }

            if (requester.Position != (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Requester Has Home", "Requester has already home");
                errors.Throw();
            }

            user = await getAdmin;

            if (isAccepted == true)
            {
                requester.Position = (int)UserPosition.HasHome;

                UserInformationModel requesterFirstName = await _userInformationRepository.GetUserInformationByIdAsync(requester.Id, (await firstNameInfo).Id);

                UserInformationModel requesterLastName = await _userInformationRepository.GetUserInformationByIdAsync(requester.Id, (await lastNameInfo).Id);

                HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

                FCMModel fcmRequester = new FCMModel(requester.DeviceId, new Dictionary <string, object>());

                fcmRequester.notification.Add("title", "Eve Katılma İsteği");
                fcmRequester.notification.Add("body", "Eve katılma isteğiniz ev yöneticisi tarafından kabul edildi.");

                await _fcmService.SendFCMAsync(fcmRequester);

                List <UserBaseModel> friendsBaseModels  = new List <UserBaseModel>();
                UserBaseModel        requesterBaseModel = new UserBaseModel(requester.Id, requester.Username, requester.Position, requesterFirstName.Value, requesterLastName.Value, 0);

                foreach (var friend in home.Users)
                {
                    FriendshipModel friendship       = new FriendshipModel(requester, friend, 0);
                    Task            insertFriendship = _friendshipRepository.InsertAsync(friendship);

                    //Sends notification to all friends
                    FCMModel fcmFriend = new FCMModel(friend.DeviceId, new Dictionary <string, object>());

                    fcmFriend.notification.Add("title", "Yeni Ev Arkadaşı");
                    fcmFriend.notification.Add("body", String.Format("{0} {1}({2}) evinize katıldı.", requesterFirstName.Value, requesterLastName.Value, requester.Username));

                    await _fcmService.SendFCMAsync(fcmFriend);

                    //Sends notification to all friends
                    fcmFriend = new FCMModel(friend.DeviceId, type: "NewFriend");
                    fcmFriend.data.Add("Friend", requesterBaseModel);

                    await _fcmService.SendFCMAsync(fcmFriend);

                    //Sends all friends to requester
                    UserInformationModel friendFirstName = await _userInformationRepository.GetUserInformationByIdAsync(friend.Id, (await firstNameInfo).Id);

                    UserInformationModel friendLastName = await _userInformationRepository.GetUserInformationByIdAsync(friend.Id, (await lastNameInfo).Id);

                    friendsBaseModels.Add(new UserBaseModel(friend.Id, friend.Username, friend.Position, friendFirstName.Value, friendLastName.Value, 0));

                    await insertFriendship;
                }

                home.Users.Add(requester);
                _homeRepository.Update(home);

                requester.Home = home;
                _userRepository.Update(requester);

                fcmRequester = new FCMModel(requester.DeviceId, type: "AllFriends");

                fcmRequester.data.Add("NumberOfFriends", home.Users.Count - 1);
                fcmRequester.data.Add("Friends", friendsBaseModels);
                await _fcmService.SendFCMAsync(fcmRequester);
            }
        }
コード例 #6
0
        //Gets user full information
        public async Task <UserFullInformationModel> GetUserFullInformationAsync(int id)
        {
            Task <InformationModel> firstNameInfo   = _informationRepository.GetInformationByInformationNameAsync("FirstName");
            Task <InformationModel> lastNameInfo    = _informationRepository.GetInformationByInformationNameAsync("LastName");
            Task <InformationModel> emailInfo       = _informationRepository.GetInformationByInformationNameAsync("Email");
            Task <InformationModel> phoneNumberInfo = _informationRepository.GetInformationByInformationNameAsync("PhoneNumber");

            UserModel user = await GetUserByIdAsync(id);

            Task <UserInformationModel> firstName   = _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await firstNameInfo).Id);
            Task <UserInformationModel> lastName    = _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await lastNameInfo).Id);
            Task <UserInformationModel> email       = _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await emailInfo).Id);
            Task <UserInformationModel> phoneNumber = _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await phoneNumberInfo).Id);

            UserFullInformationModel fullInfo = new UserFullInformationModel();

            fullInfo.User.Id       = user.Id;
            fullInfo.User.Username = user.Username;
            fullInfo.User.Position = user.Position;
            fullInfo.User.Debt     = 0;
            fullInfo.Token         = user.Token;

            fullInfo.User.FirstName = (await firstName).Value;
            fullInfo.User.LastName  = (await lastName).Value;
            fullInfo.Email          = (await email).Value;
            fullInfo.PhoneNumber    = (await phoneNumber).Value;

            if (user.Position == (int)UserPosition.HasNotHome)
            {
                fullInfo.NumberOfFriends = 0;
                fullInfo.Friends         = null;
                fullInfo.HomeName        = null;
            }
            else
            {
                user = await GetUserByIdAsync(id, true);

                HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

                fullInfo.HomeName        = home.Name;
                fullInfo.NumberOfFriends = home.Users.Count - 1;

                foreach (var friend in home.Users)
                {
                    if (friend.Equals(user))
                    {
                        continue;
                    }

                    string friendFirstName = (await _userInformationRepository.GetUserInformationByIdAsync(friend.Id, (await firstNameInfo).Id)).Value;
                    string friendLastName  = (await _userInformationRepository.GetUserInformationByIdAsync(friend.Id, (await lastNameInfo).Id)).Value;

                    double          debt       = 0;
                    FriendshipModel friendship = await _friendshipRepository.GetFriendshipByIdAsync(user.Id, friend.Id);

                    if (friendship == null)
                    {
                        CustomException errors = new CustomException();
                        errors.AddError("Unexpected Error Occured", "Friendship does not exist");
                        errors.Throw();
                    }

                    if (friendship.User1.Id == user.Id)
                    {
                        debt = friendship.Debt;
                    }
                    else
                    {
                        debt = -friendship.Debt;
                    }

                    fullInfo.Friends.Add(new UserBaseModel(friend.Id, friend.Username, friend.Position, friendFirstName, friendLastName, debt));
                }
            }

            return(fullInfo);
        }
コード例 #7
0
        //User adds expense
        public async Task AddExpenseAsync(UserModel user, ExpenseModel expense, List <int> participants)
        {
            if (user.Position == (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Home Not Exist", "User is not member of a home");
                errors.Throw();
            }

            if ((expense.EType > (int)ExpenseType.Others) || (expense.EType < 0))
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Expense Type Not Exist", "Expense type number not valid");
                errors.Throw();
            }

            if (expense.Cost <= 0)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Expense Cost Not Valid", "The expense cost must be bigger than 0");
                errors.Throw();
            }

            participants = participants.Distinct().ToList();

            if (participants.Count == 0)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Participants Not Exist", "You must add participants for this expense");
                errors.Throw();
            }

            Task <InformationModel> firstNameInfo = _informationRepository.GetInformationByInformationNameAsync("FirstName");
            Task <InformationModel> lastNameInfo  = _informationRepository.GetInformationByInformationNameAsync("LastName");

            user = await _userRepository.GetByIdAsync(user.Id, true);

            HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

            //Friend not found
            foreach (var p in participants)
            {
                if (home.Users.SingleOrDefault(u => u.Id == p) == null)
                {
                    CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                    errors.AddError("Friendship Not Found", "Friendship not found for lend");
                    errors.Throw();
                }
            }

            expense.LastUpdated = DateTime.UtcNow;
            expense.Home        = home;
            expense.Author      = await _userRepository.GetByIdAsync(user.Id);;

            //Author informations
            UserInformationModel userFirstName = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await firstNameInfo).Id);

            UserInformationModel userLastName = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await lastNameInfo).Id);

            //Lend
            if (expense.EType == (int)ExpenseType.Lend)
            {
                if (participants.SingleOrDefault(p => p.Equals(user.Id)) != 0)
                {
                    CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                    errors.AddError("Lend Error", "You cant lend yourself");
                    errors.Throw();
                }

                ExpenseModel borrowExpense = new ExpenseModel((int)ExpenseType.Borrow,
                                                              expense.Cost / participants.Count,
                                                              expense.Author,
                                                              expense.Home,
                                                              expense.LastUpdated,
                                                              expense.Title,
                                                              expense.Content);

                _expenseRepository.Insert(expense);
                _expenseRepository.Insert(borrowExpense);

                //Borrow for all participants
                foreach (var p in participants)
                {
                    var to = await _userRepository.GetByIdAsync(p);

                    await _homeService.TransferMoneyToFriendAsync(user, to, borrowExpense.Cost);

                    await _userExpenseRepository.InsertAsync(new UserExpenseModel(to, borrowExpense));

                    //Send fcm to other participants
                    FCMModel fcmBorrow = new FCMModel(to.DeviceId, new Dictionary <string, object>());
                    fcmBorrow.notification.Add("title", "Nakit Aktarımı");
                    fcmBorrow.notification.Add("body", String.Format("{0} {1} tarafından {2:c} nakit alındı.",
                                                                     userFirstName.Value,
                                                                     userLastName.Value,
                                                                     borrowExpense.Cost));
                    await _fcmService.SendFCMAsync(fcmBorrow);

                    //Send fcm to other participants
                    fcmBorrow = new FCMModel(to.DeviceId, type: "AddExpense");
                    fcmBorrow.data.Add("Content", borrowExpense);
                    fcmBorrow.data.Add("Author", expense.Author.Username);
                    fcmBorrow.data.Add("Participants", participants);

                    await _fcmService.SendFCMAsync(fcmBorrow);
                }

                Task insertUEL = _userExpenseRepository.InsertAsync(new UserExpenseModel(user, expense));

                //Send fcm to user
                FCMModel fcmLend = new FCMModel(user.DeviceId, type: "AddExpense");
                fcmLend.data.Add("Content", expense);
                fcmLend.data.Add("Author", expense.Author.Username);
                fcmLend.data.Add("Participants", participants);

                await _fcmService.SendFCMAsync(fcmLend);

                await insertUEL;
            }

            else
            {
                expense.Cost = expense.Cost / participants.Count;
                _expenseRepository.Insert(expense);

                foreach (var p in participants)
                {
                    //Paid for friends
                    if (p != user.Id)
                    {
                        var to = await _userRepository.GetByIdAsync(p);

                        await _homeService.TransferMoneyToFriendAsync(user, to, expense.Cost);

                        await _userExpenseRepository.InsertAsync(new UserExpenseModel(to, expense));

                        //Send fcm to other participants
                        FCMModel fcmExpense = new FCMModel(to.DeviceId, new Dictionary <string, object>());
                        fcmExpense.notification.Add("title", String.Format("Yeni Gider : \"{0}\"", expense.Title));
                        fcmExpense.notification.Add("body", String.Format("{0} {1} tarafından {2:c} ödendi.",
                                                                          userFirstName.Value,
                                                                          userLastName.Value,
                                                                          expense.Cost));

                        await _fcmService.SendFCMAsync(fcmExpense);

                        //Send fcm to other participants
                        fcmExpense = new FCMModel(to.DeviceId, type: "AddExpense");

                        fcmExpense.data.Add("Content", expense);
                        fcmExpense.data.Add("Author", expense.Author.Username);
                        fcmExpense.data.Add("Participants", participants);

                        await _fcmService.SendFCMAsync(fcmExpense);
                    }
                    else
                    {
                        Task insertUE = _userExpenseRepository.InsertAsync(new UserExpenseModel(user, expense));

                        //Send fcm to user
                        FCMModel fcmExpense = new FCMModel(user.DeviceId, type: "AddExpense");
                        fcmExpense.data.Add("Content", expense);
                        fcmExpense.data.Add("Author", expense.Author.Username);
                        fcmExpense.data.Add("Participants", participants);

                        await _fcmService.SendFCMAsync(fcmExpense);

                        await insertUE;
                    }
                }
                //Expense author is not exist in participants
                if (participants.SingleOrDefault(pr => pr == user.Id) == 0)
                {
                    //Send fcm to user
                    FCMModel fcmExpense = new FCMModel(user.DeviceId, type: "AddExpense");
                    fcmExpense.data.Add("Content", expense);
                    fcmExpense.data.Add("Author", expense.Author.Username);
                    fcmExpense.data.Add("Participants", participants);

                    await _fcmService.SendFCMAsync(fcmExpense);
                }
            }
        }