//Updates shopping list public async Task UpdateShoppingListAsync(UserModel user, ShoppingListModel shoppingList) { 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); ShoppingListModel old = await _shoppingListRepository.GetShoppingListByHomeIdAsync(user.Home.Id, true); if (old == null) { CustomException errors = new CustomException((int)HttpStatusCode.BadRequest); errors.AddError("Shopping List Not Exist", "Shopping list is not exist"); errors.Throw(); } old.List = shoppingList.List; old.Status = shoppingList.Status; _shoppingListRepository.Update(old); foreach (var friend in user.Home.Users) { FCMModel fcm = new FCMModel(friend.DeviceId, type: "ShoppingListUpdate"); fcm.data.Add("UpdatedShoppingList", old); await _fcmService.SendFCMAsync(fcm); } }
//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); } }
//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); } }
//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); } }
//User requests to admin for joining home public async Task JoinHomeRequestAsync(UserModel user, string joinHomeName) { Task <InformationModel> firstNameInfo = _informationRepository.GetInformationByInformationNameAsync("FirstName"); Task <InformationModel> lastNameInfo = _informationRepository.GetInformationByInformationNameAsync("LastName"); if (user.Position != (int)UserPosition.HasNotHome) { CustomException errors = new CustomException((int)HttpStatusCode.BadRequest); errors.AddError("User Has Home", "User has home already"); errors.Throw(); } var home = await _homeRepository.GetByHomeNameAsync(joinHomeName, true); if (home == null) { CustomException errors = new CustomException((int)HttpStatusCode.BadRequest); errors.AddError("Home Name Not Exist", "Home name has not exist"); errors.Throw(); } //User waiting for admin's accept UserInformationModel firstName = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await firstNameInfo).Id); UserInformationModel lastName = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await lastNameInfo).Id); FCMModel fcm = new FCMModel(home.Admin.DeviceId, new Dictionary <string, object>()); fcm.notification.Add("title", "Eve Katılma İsteği"); fcm.notification.Add("body", String.Format("{0} {1}({2}) evinize katılmak istiyor.", firstName.Value, lastName.Value, user.Username)); await _fcmService.SendFCMAsync(fcm); fcm = new FCMModel(home.Admin.DeviceId, type: "JoinHomeRequest"); fcm.data.Add("RequesterId", user.Id); fcm.data.Add("RequesterUsername", user.Username); fcm.data.Add("RequesterName", firstName.Value); fcm.data.Add("RequesterLastName", lastName.Value); await _fcmService.SendFCMAsync(fcm); }
//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); } } }