private void RegisterUserDetailed(string email, string password, int cityId) { Guid userGuid = System.Guid.NewGuid(); string hashedPassword = Security.HashSHA1(password + userGuid.ToString()); User comment = new User() { Email = email, Password = hashedPassword, UserGuid = userGuid, Id_city = cityId, Account_registration_date = DateTime.Now.ToLocalTime().ToLocalTime(), }; try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { if (db.Users.Any(i => i.Email == email)) { throw new Exception("Podana nazwa jest zajęta! Spróbuj innej."); } db.Users.Add(comment); db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private void EditCityDetailed(string cityName, int provinceId, int cityId, double longitude, double latitude, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } City city = db.Cities.FirstOrDefault(i => i.Id_city == cityId); city.Name = cityName; city.Id_province = provinceId; city.Longitude = longitude; city.Latitude = latitude; db.Cities.Attach(city); db.Entry(city).State = EntityState.Modified; db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public IEnumerable <object> GetMoneyIncludes() { List <object> finalResultList = new List <object>(); try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { foreach (var item in db.Pocket_money_options) { finalResultList.Add(new { id = item.Id_pocket_money_option, name = item.Name.Trim(), }); } return(finalResultList); } } catch (Exception) { return(null); } }
public IEnumerable <object> GetPaymentPeriodDictionary() { List <object> finalResultList = new List <object>(); try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { foreach (var item in db.Payout_periods) { finalResultList.Add(new { id = item.Id_payout_period, days = item.Days, name = item.Name.Trim(), }); } return(finalResultList); } } catch (Exception) { return(null); } }
private void DeletePaymentPeriodDetailed(int paymentPeriodId, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } Payout_period payoutPeriod = db.Payout_periods.FirstOrDefault(i => i.Id_payout_period == paymentPeriodId); var children = db.Children.Where(i => i.Id_payout_period == paymentPeriodId).Include(i => i.Child_pocket_money_option); foreach (var child in children) { db.Children.Remove(child); } db.Payout_periods.Remove(payoutPeriod); db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private void DeleteSchoolTypeDetailed(int schoolTypeId, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } Education_stage educationStage = db.Education_stages.FirstOrDefault(i => i.Id_education_stage == schoolTypeId); var children = db.Children.Where(i => i.Id_education_stage == schoolTypeId).Include(i => i.Child_pocket_money_option); foreach (var child in children) { db.Children.Remove(child); } db.Education_stages.Remove(educationStage); db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public IEnumerable <object> GetCityDictionary() { List <object> finalResultList = new List <object>(); try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { foreach (var item in db.Cities) { finalResultList.Add(new { id = item.Id_city, provinceId = item.Id_province, name = item.Name.Trim(), longitude = item.Longitude, latitude = item.Latitude, }); } return(finalResultList); } } catch (Exception) { return(null); } }
private void ChangePasswordDetailed(string newPassword, string token) { try { int userId; if (Security.UserTokens.Any(i => i.Value == token)) { userId = Security.UserTokens.FirstOrDefault(i => i.Value == token).Key; } else { throw new Exception("Identyfikacja użytkownika nie powiodła się"); } using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { User user = db.Users.FirstOrDefault(i => i.Id_user == userId); string hashedPassword = Security.HashSHA1(newPassword + user.UserGuid.ToString()); user.Password = hashedPassword; db.Users.Attach(user); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public IEnumerable <object> GetSchoolTypeDictionary() { List <object> finalResultList = new List <object>(); try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { foreach (var item in db.Education_stages) { finalResultList.Add(new { id = item.Id_education_stage, name = item.Name.Trim(), }); } return(finalResultList); } } catch (Exception) { return(null); } }
private void AddCityDetailed(string cityName, int provinceId, double longitude, double latitude, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } db.Cities.Add(new City { Name = cityName, Id_province = provinceId, Longitude = longitude, Latitude = latitude }); db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private void AddCommentDetailed(int cityId, int provinceId, string content, int userId) { Comment comment = new Comment() { Content = content, Id_user = userId, Creation_date = DateTime.Now.ToLocalTime(), Likes_amount = 0, }; if (cityId != -1) { comment.Id_city = cityId; } if (provinceId != -1) { comment.Id_province = provinceId; } int asd; try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { db.Comments.Add(comment); db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private void DeleteMoneyIncludesDetailed(int moneyIncludesId, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } Pocket_money_option pocketMoneyOption = db.Pocket_money_options.FirstOrDefault(i => i.Id_pocket_money_option == moneyIncludesId); var children = db.Children.Where(i => i.Child_pocket_money_option.Any(j => j.Id_pocket_money_option == moneyIncludesId)).Include(i => i.Child_pocket_money_option); foreach (var child in children) { db.Children.Remove(child); } db.Pocket_money_options.Remove(pocketMoneyOption); db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private IEnumerable <object> DeleteChildDetailed(int childId, string token) { try { int userId; if (Security.UserTokens.Any(i => i.Value == token)) { userId = Security.UserTokens.FirstOrDefault(i => i.Value == token).Key; } else { throw new Exception("Identyfikacja użytkownika nie powiodła się"); } using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { Child child = db.Children.FirstOrDefault(i => i.Id_child == childId); db.Child_pocket_money_options.RemoveRange(child.Child_pocket_money_option); db.Children.Remove(child); db.SaveChanges(); var children = db.Children.Where(i => i.Id_user == userId); var userChildrenList = new List <object>(); List <object> moneyIncludesList; foreach (var singleChild in children) { moneyIncludesList = new List <object>(); foreach (var item in singleChild.Child_pocket_money_option) { moneyIncludesList.Add(item.Id_pocket_money_option); } userChildrenList.Add(new { id = singleChild.Id_child, name = singleChild.First_name.Trim(), age = DateTime.Now.ToLocalTime().ToLocalTime().ToLocalTime().Year - singleChild.Date_of_birth.Value.Year, dateOfBirth = singleChild.Date_of_birth?.ToString("yyyy-MM-dd"), schoolTypeId = singleChild.Id_education_stage, quota = singleChild.Current_amount_of_money, paymentPeriodId = singleChild.Id_payout_period, paymentDate = singleChild.Date_of_payout.ToString("yyyy-MM-dd"), prevPaymentDate = singleChild.Date_of_payout.PreviousPaymentDate(singleChild.Payout_period.Days)?.ToString("yyyy-MM-dd"), nextPaymentDate = singleChild.Date_of_payout.NextPaymentDate(singleChild.Payout_period.Days)?.ToString("yyyy-MM-dd"), provinceId = db.Cities.FirstOrDefault(i => i.Id_city == singleChild.Id_city).Id_province, cityId = singleChild.Id_city, moneyIncludes = moneyIncludesList }); } return(userChildrenList); } } catch (Exception ex) { throw ex; } }
private object ChangeMetaNotificationDetailed(bool isSubscribed, string token) { try { int userId; if (Security.UserTokens.Any(i => i.Value == token)) { userId = Security.UserTokens.FirstOrDefault(i => i.Value == token).Key; } else { throw new Exception("Identyfikacja użytkownika nie powiodła się"); } using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { Information_notification informationNotification = db.Information_notifications.FirstOrDefault(i => i.Id_user == userId); if (informationNotification == null) { db.Information_notifications.Add(new Information_notification { Id_user = userId, }); } else if (isSubscribed == false) { db.Information_notifications.Remove(informationNotification); } db.SaveChanges(); var finalResult = new { success = true, message = String.Empty, userMetaNotification = isSubscribed }; return(finalResult); } } catch (Exception ex) { var finalResult = new { success = false, message = ex.Message }; return(finalResult); } }
private object ChangeUserDataDetailed(int cityId, string token) { string errorMessage = string.Empty; try { int userId; if (Security.UserTokens.Any(i => i.Value == token)) { userId = Security.UserTokens.FirstOrDefault(i => i.Value == token).Key; } else { throw new Exception("Identyfikacja użytkownika nie powiodła się"); } using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { User user = db.Users.FirstOrDefault(i => i.Id_user == userId); user.Id_city = cityId; db.Users.Attach(user); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); var finalResult = new { success = true, message = String.Empty, email = user.Email.Trim(), accountActivationDate = user.Account_registration_date.ToString("yyyy-MM-dd"), accountLastLogInDate = user.Last_login_date?.ToString("yyyy-MM-dd;HH-mm"), provinceId = user.City.Id_province, cityId = user.Id_city, province = user.City.Province.Name.Trim(), city = user.City.Name.Trim(), }; return(finalResult); } } catch (Exception ex) { var finalResult = new { success = false, message = ex.Message }; return(finalResult); } }
private IEnumerable <object> AddNotificationDetailed(int childId, int notificationOverLap, string token) { try { int userId; if (Security.UserTokens.Any(i => i.Value == token)) { userId = Security.UserTokens.FirstOrDefault(i => i.Value == token).Key; } else { throw new Exception("Identyfikacja użytkownika nie powiodła się"); } using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { Reminder_notification reminderNotification = new Reminder_notification { Id_child = childId, Id_user = userId, Days_number = notificationOverLap }; db.Reminder_notifications.Add(reminderNotification); db.SaveChanges(); var reminderNotificaions = db.Reminder_notifications.Where(i => i.Id_user == userId); var reminderNotificaionsList = new List <object>(); foreach (var singleReminderNotification in reminderNotificaions) { reminderNotificaionsList.Add(new { id = singleReminderNotification.Id_reminder_notification, kidId = singleReminderNotification.Id_child, notificationOverlap = singleReminderNotification.Days_number, }); } return(reminderNotificaionsList); } } catch (Exception ex) { throw ex; } }
private object ResetPasswordDetailed(string email) { string errorMessage = string.Empty; try { string newPassword; newPassword = Security.GeneratePassword(15); User user; User admin; using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { user = db.Users.FirstOrDefault(i => i.Email.Trim() == email); string hashedPassword = Security.HashSHA1(newPassword + user.UserGuid); user.Password = hashedPassword; db.Users.Attach(user); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); admin = db.Users.FirstOrDefault(i => i.Email.Trim() == "*****@*****.**"); } string hashedPasswordAdmin = Security.HashSHA1(admin.Password + admin.UserGuid); Email.SendEmail(user.Email.Trim(), $"Twoje nowe hasło: {newPassword}", "Zmiana hasła", hashedPasswordAdmin); var finalResult = new { success = true, message = errorMessage }; return(finalResult); } catch (Exception ex) { var finalResult = new { success = false, message = ex.Message }; return(finalResult); } }
private void AddMoneyIncludesDetailed(string moneyIncludesName, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } db.Pocket_money_options.Add(new Pocket_money_option { Name = moneyIncludesName }); db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private void AddSchoolTypeDetailed(string schoolTypeName, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } db.Education_stages.Add(new Education_stage { Name = schoolTypeName }); db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private void EditSchoolTypeDetailed(string schoolTypeName, int schoolTypeId, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } Education_stage educationStage = db.Education_stages.FirstOrDefault(i => i.Id_education_stage == schoolTypeId); educationStage.Name = schoolTypeName; db.Education_stages.Attach(educationStage); db.Entry(educationStage).State = EntityState.Modified; db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private void EditProvinceDetailed(string provinceName, int provinceId, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } Province province = db.Provinces.FirstOrDefault(i => i.Id_province == provinceId); province.Name = provinceName; db.Provinces.Attach(province); db.Entry(province).State = EntityState.Modified; db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private void EditMoneyIncludesDetailed(string moneyIncludesName, int moneyIncludesId, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } Pocket_money_option pocketMoneyOption = db.Pocket_money_options.FirstOrDefault(i => i.Id_pocket_money_option == moneyIncludesId); pocketMoneyOption.Name = moneyIncludesName; db.Pocket_money_options.Attach(pocketMoneyOption); db.Entry(pocketMoneyOption).State = EntityState.Modified; db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private void EditPaymentPeriodDetailed(string paymentPeriodName, int days, int paymentPeriodId, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } Payout_period payoutPeriod = db.Payout_periods.FirstOrDefault(i => i.Id_payout_period == paymentPeriodId); payoutPeriod.Name = paymentPeriodName; payoutPeriod.Days = days; db.Payout_periods.Attach(payoutPeriod); db.Entry(payoutPeriod).State = EntityState.Modified; db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private void AddPaymentPeriodDetailed(string paymentPeriodName, int days, int userId) { try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var isAdmin = db.Users.FirstOrDefault(i => i.Id_user == userId).IsAdmin; if (!isAdmin) { throw new Exception("Nie posiadasz wystarczających uprawnień."); } db.Payout_periods.Add(new Payout_period { Name = paymentPeriodName, Days = days }); db.SaveChanges(); } } catch (Exception ex) { throw ex; } }
private IEnumerable <object> GetCommentsDetailed(int cityId, int provinceId, int userId) { IEnumerable <Comment> cityComments; var finalResultComments = new List <object>(); using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { if (cityId == -1 && provinceId == -1) { cityComments = db.Comments.Where(i => i.Id_city == null && i.Id_province == null); } else if (cityId == -1) { cityComments = db.Comments.Where(i => i.Id_city == null && i.Id_province == provinceId); } else { cityComments = db.Comments.Where(i => i.Id_city == cityId && i.Id_province == provinceId); } foreach (var item in cityComments) { bool isLiked = db.Likes.Any(i => userId == i.Id_user && i.Id_comment == item.Id_comment); finalResultComments.Add(new { id = item.Id_comment, author = item.User.Email, content = item.Content.Trim(), upvotes = item.Likes_amount, liked = isLiked }); } } return(finalResultComments); }
private object GetCountryStatsDetailed(bool useFilters, int ageRangeMin, int ageRangeMax, string moneyIncludes, int schoolTypeId, bool filterByMoneyIncludes) { List <int> moneyIncludesArray = new List <int>(); int amountOfElements = 0; if (useFilters) { string[] tmp = null; string tmpWithComma; string[] finalArray = null; if (moneyIncludes != null) { tmp = Regex.Split(moneyIncludes, "%2C"); tmpWithComma = string.Join("", tmp); finalArray = tmpWithComma.Split(','); } moneyIncludesArray = new List <int>(); if (finalArray != null) { foreach (var item in finalArray) { moneyIncludesArray.Add(Int32.Parse(item)); } amountOfElements = moneyIncludesArray.Count; } } try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { var children = db.Children.ToList(); if (useFilters) { // age filter var today = DateTime.Today; children = children.Where(i => (today.Year - i.Date_of_birth.Value.Year >= ageRangeMin) && (today.Year - i.Date_of_birth.Value.Year <= ageRangeMax)).ToList(); // school type filter if (schoolTypeId != -1) { children = children.Where(i => i.Id_education_stage == schoolTypeId).ToList(); } // moneyIncludes filter if (filterByMoneyIncludes) { List <Pocket_money_option> pocketMoneyOptions = new List <Pocket_money_option>(); foreach (var child in children.ToList()) { var idPocketMoney = child.Child_pocket_money_option.Select(i => i.Id_pocket_money_option).ToList(); var result = moneyIncludesArray.Concat(idPocketMoney); var finalResultOfSets = result.Union(moneyIncludesArray); if (!moneyIncludesArray.SequenceEqual(child.Child_pocket_money_option.Select(i => i.Id_pocket_money_option))) { children.Remove(child); } } } } double average, sumOfSquaresOfDifferences, sd; average = sumOfSquaresOfDifferences = sd = 0; if (children.Count > 0) { average = (double)children.Average(i => (i.Current_amount_of_money / i.Payout_period.Days) * 30); sumOfSquaresOfDifferences = children. Select(i => ((double)i.Current_amount_of_money - average) * ((double)i.Current_amount_of_money - average)).Sum(); sd = Math.Sqrt(sumOfSquaresOfDifferences / children.Count()); } var finalResult = new { id = 0, name = "Polska", avg = average, std = sd, count = children.Count() }; return(finalResult); } } catch (Exception ex) { throw ex; } }
private object ValidateUserDetailed(string email, string password) { string errorMessage = string.Empty; try { using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { User user = db.Users.FirstOrDefault(i => i.Email == email); if (user == null) { throw new UserNotFoundException("Nie ma takiego użytkownika."); } string hashedPassword = Security.HashSHA1(password + user.UserGuid); // if its correct password the result of the hash is the same as in the database if (user.Password.Trim() == hashedPassword) { // The password is correct string generatedToken = Security.GenerateToken(user.Email); if (!Security.UserTokens.Any(i => i.Key == user.Id_user)) { Security.UserTokens.Add(user.Id_user, generatedToken); } user.Last_login_date = DateTime.Now; db.Users.Attach(user); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); var userChildrenList = new List <object>(); var children = user.Child.Where(i => i.Id_user == user.Id_user); List <object> moneyIncludesList; foreach (var child in children) { moneyIncludesList = new List <object>(); foreach (var item in child.Child_pocket_money_option) { moneyIncludesList.Add(item.Id_pocket_money_option); } userChildrenList.Add(new { id = child.Id_child, name = child.First_name.Trim(), age = DateTime.Now.ToLocalTime().ToLocalTime().Year - child.Date_of_birth.Value.Year, dateOfBirth = child.Date_of_birth?.ToString("yyyy-MM-dd"), schoolTypeId = child.Id_education_stage, quota = child.Current_amount_of_money, paymentPeriodId = child.Id_payout_period, paymentDate = child.Date_of_payout.ToString("yyyy-MM-dd"), prevPaymentDate = child.Date_of_payout.PreviousPaymentDate(child.Payout_period.Days)?.ToString("yyyy-MM-dd"), nextPaymentDate = child.Date_of_payout.NextPaymentDate(child.Payout_period.Days)?.ToString("yyyy-MM-dd"), provinceId = child.City.Id_province, cityId = child.Id_city, moneyIncludes = moneyIncludesList }); } var reminderNotifications = user.Reminder_notification.Where(i => i.Id_user == user.Id_user); List <object> reminderNotificationsList = new List <object>(); foreach (var reminderNotification in reminderNotifications) { reminderNotificationsList.Add(new { id = reminderNotification.Id_reminder_notification, kidId = reminderNotification.Id_child, notificationOverlap = reminderNotification.Days_number, }); } var userMetaNotificationElement = user.Information_notification.Any(i => i.Id_user == user.Id_user); var finalResult = new { success = true, message = String.Empty, isValidated = true, isAdmin = user.IsAdmin, token = Security.UserTokens.FirstOrDefault(i => i.Key == user.Id_user).Value, userData = new { email = user.Email.Trim(), accountActivationDate = user.Account_registration_date.ToString("yyyy-MM-dd"), accountLastLogInDate = user.Last_login_date?.ToString("yyyy-MM-dd;HH-mm"), provinceId = user.City.Id_province, cityId = user.Id_city, province = user.City.Province.Name.Trim(), city = user.City.Name.Trim(), }, userKids = userChildrenList, userNotifications = reminderNotificationsList, userMetaNotification = userMetaNotificationElement }; return(finalResult); } else { var finalResult = new { success = false, message = "Błędne hasło" }; return(finalResult); } } } catch (Exception ex) { var finalResult = new { success = false, message = ex.Message }; return(finalResult); } }
private object ToggleCommentUpvoteDetailed(int commentId, bool isLiked, string token) { int userId; try { if (Security.UserTokens.Any(i => i.Value == token)) { userId = Security.UserTokens.FirstOrDefault(i => i.Value == token).Key; } else { throw new Exception("Identyfikacja użytkownika nie powiodła się"); } using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { Comment comment = db.Comments.FirstOrDefault(i => i.Id_comment == commentId); Like like; int? cityId, provinceId; if (isLiked) { comment.Likes_amount = comment.Likes_amount + 1; db.Comments.Attach(comment); db.Entry(comment).State = EntityState.Modified; like = new Like { Amount_of_likes = 1, Id_user = userId, Id_comment = comment.Id_comment }; db.Likes.Add(like); db.SaveChanges(); like = db.Likes.FirstOrDefault(i => i.Id_comment == commentId && i.Id_user == userId); cityId = like.Comment.Id_city; provinceId = like.Comment.Id_province; } else { comment.Likes_amount--; db.Comments.Attach(comment); db.Entry(comment).State = EntityState.Modified; like = db.Likes.FirstOrDefault(i => i.Id_comment == commentId && i.Id_user == userId); cityId = like.Comment.Id_city; provinceId = like.Comment.Id_province; db.Likes.Remove(like); } db.SaveChanges(); if (cityId == null) { cityId = -1; } if (provinceId == null) { provinceId = -1; } return(GetComments(cityId.Value, provinceId.Value, token)); } } catch (Exception ex) { var finalResult = new { success = false, message = ex.Message, }; return(finalResult); } }
private IEnumerable <object> EditChildDetailed(int childId, DateTime dateOfBirth, string name, double quota, int cityId, string moneyIncludes, DateTime paymentDate, int paymentPeriodId, int schoolTypeId, string token) { List <Child_pocket_money_option> childPocketMoneyOptions = new List <Child_pocket_money_option>(); try { int userId; if (Security.UserTokens.Any(i => i.Value == token)) { userId = Security.UserTokens.FirstOrDefault(i => i.Value == token).Key; } else { throw new Exception("Identyfikacja użytkownika nie powiodła się"); } using (JakieKieszonkoweEntities db = new JakieKieszonkoweEntities()) { string[] tmp = Regex.Split(moneyIncludes, "%2C"); string tmpWithComma = string.Join("", tmp); string[] finalArray = tmpWithComma.Split(','); List <int> moneyIncludesArray = new List <int>(); foreach (var item in finalArray) { moneyIncludesArray.Add(Int32.Parse(item)); } Child child = db.Children.FirstOrDefault(i => i.Id_child == childId); if (moneyIncludesArray != null) { foreach (int id in moneyIncludesArray) { Child_pocket_money_option childPocketMoneyOption = new Child_pocket_money_option { Id_child = child.Id_child, Id_pocket_money_option = id, }; childPocketMoneyOptions.Add(childPocketMoneyOption); } } db.Child_pocket_money_options.RemoveRange(child.Child_pocket_money_option); child.First_name = name; child.Current_amount_of_money = (decimal)quota; child.Id_city = cityId; child.Date_of_birth = dateOfBirth; child.Id_education_stage = schoolTypeId; child.Child_pocket_money_option = childPocketMoneyOptions; child.Id_payout_period = paymentPeriodId; child.Date_of_payout = paymentDate; db.Children.Attach(child); db.Entry(child).State = EntityState.Modified; db.SaveChanges(); var children = db.Children.Where(i => i.Id_user == userId); var userChildrenList = new List <object>(); List <object> moneyIncludesList; foreach (var singleChild in children) { moneyIncludesList = new List <object>(); foreach (var item in singleChild.Child_pocket_money_option) { moneyIncludesList.Add(item.Id_pocket_money_option); } userChildrenList.Add(new { id = singleChild.Id_child, name = singleChild.First_name.Trim(), age = DateTime.Now.ToLocalTime().ToLocalTime().ToLocalTime().Year - singleChild.Date_of_birth.Value.Year, dateOfBirth = singleChild.Date_of_birth?.ToString("yyyy-MM-dd"), schoolTypeId = singleChild.Id_education_stage, quota = singleChild.Current_amount_of_money, paymentPeriodId = singleChild.Id_payout_period, paymentDate = singleChild.Date_of_payout.ToString("yyyy-MM-dd"), prevPaymentDate = singleChild.Date_of_payout.PreviousPaymentDate(db.Payout_periods.FirstOrDefault(i => i.Id_payout_period == singleChild.Id_payout_period).Days)?.ToString("yyyy-MM-dd"), nextPaymentDate = singleChild.Date_of_payout.NextPaymentDate(db.Payout_periods.FirstOrDefault(i => i.Id_payout_period == singleChild.Id_payout_period).Days)?.ToString("yyyy-MM-dd"), provinceId = db.Cities.FirstOrDefault(i => i.Id_city == singleChild.Id_city).Id_province, cityId = singleChild.Id_city, moneyIncludes = moneyIncludesList }); } return(userChildrenList); } } catch (Exception ex) { throw ex; } }