/// <summary> /// Try to add Expense Type to given Category with given name to user /// </summary> /// <param name="newETypeName"></param> /// <param name="userID"></param> /// <param name="cat"></param> ///<returns>returns 1 if success, 0 if fail because ExpenseType already exists</returns> internal static int AddExpenseTypetoCategory(string newETypeName, int userID, string cat) { string nameCaseCorrected = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(newETypeName.ToLower()); using (var ctx = new BudgetContext()) { Console.WriteLine("\n\n\n\n\n" + cat + "\n\n\n\n\n"); ExpenseType eType = ctx.ExpenseTypes.FirstOrDefault(et => et.ExpenseTypeName == nameCaseCorrected && et.ExpenseCategory.User_ID == userID); ExpenseCategory eCat = ctx.ExpenseCategories.FirstOrDefault(ec => ec.ExpenseCategoryName == cat && ec.User_ID == userID); if (eType == null) { ctx.ExpenseTypes.Add(new ExpenseType() { ExpenseTypeName = nameCaseCorrected, ExpenseCategory = eCat }); ctx.SaveChanges(); return(1); } return(0); } }
internal static void DeleteIncome(int incID) { using (var ctx = new BudgetContext()) { ctx.Incomes.Remove(ctx.Incomes.Find(incID)); ctx.SaveChanges(); } }
internal static void DeleteExpense(int expID) { using (var ctx = new BudgetContext()) { ctx.Expenses.Remove(ctx.Expenses.Find(expID)); ctx.SaveChanges(); } }
internal static void RemoveEmployee(int employerID, int employeeID) { using (var ctx = new BudgetContext()) { User Employer = ctx.Users.Find(employerID); User Employee = ctx.Users.Find(employeeID); Employee.Employer = null; ctx.SaveChanges(); } }
/// <summary> /// Adds Expense to User to given date /// </summary> /// <param name="amount">decimal amount of expense</param> /// <param name="eTypeStr">Expense Type</param> /// <param name="userID"></param> /// <param name="dt">Date of expense</param> internal static void AddExpense(decimal amount, string eTypeStr, int userID, DateTime dt) { using (var ctx = new BudgetContext()) { ExpenseType eType = ctx.ExpenseTypes.FirstOrDefault(et => et.ExpenseTypeName == eTypeStr); User usr = ctx.Users.Find(userID); ctx.Expenses.Add(new Expense() { Amount = amount, Date = dt, ExpenseType = eType, User = usr }); ctx.SaveChanges(); } }
/// <summary> /// Add income to given date /// </summary> /// <param name="amount"></param> /// <param name="iTypeStr">Income Type Name</param> /// <param name="userID"></param> /// <param name="dt">Date</param> internal static void AddIncome(decimal amount, string iTypeStr, int userID, DateTime dt) { using (var ctx = new BudgetContext()) { IncomeType iType = ctx.IncomeTypes.FirstOrDefault(it => it.IncomeTypeName == iTypeStr); User usr = ctx.Users.Find(userID); ctx.Incomes.Add(new Income() { Amount = amount, Date = dt, IncomeType = iType, User = usr }); ctx.SaveChanges(); } }
internal static void DeleteIncomesOfType(string iType, int userID) { using (var ctx = new BudgetContext()) { foreach (Income i in ctx.Incomes) { if (i.IncomeType.IncomeTypeName == iType && i.User.UserID == userID) { ctx.Incomes.Remove(i); } } ctx.SaveChanges(); } }
internal static void DeleteExpensesOfType(string eType, int userID) { using (var ctx = new BudgetContext()) { foreach (Expense e in ctx.Expenses) { if (e.ExpenseType.ExpenseTypeName == eType && e.User.UserID == userID) { ctx.Expenses.Remove(e); } } ctx.SaveChanges(); } }
internal static void DeleteTypesAndExpensesofExpenseCategory(string catName, int userID) { using (var ctx = new BudgetContext()) { foreach (ExpenseType et in ctx.ExpenseTypes) { if (et.ExpenseCategory.ExpenseCategoryName == catName && et.ExpenseCategory.User_ID == userID) { DeleteExpensesOfType(et.ExpenseTypeName, userID); ctx.ExpenseTypes.Remove(et); } } ctx.SaveChanges(); } }
internal static void DeleteTypesAndIncomesofIncomeCategory(string catName, int userID) { using (var ctx = new BudgetContext()) { foreach (IncomeType it in ctx.IncomeTypes) { if (it.IncomeCategory.IncomeCategoryName == catName && it.IncomeCategory.User_ID == userID) { DeleteIncomesOfType(it.IncomeTypeName, userID); ctx.IncomeTypes.Remove(it); } } ctx.SaveChanges(); } }
/// <summary> /// /// </summary> /// <param name="itName"></param> /// <param name="userID"></param> ///<returns>returns 1 if success, 0 if fail because IncomeType does not exists</returns> internal static int DeleteIncomeType(string itName, int userID) { using (var ctx = new BudgetContext()) { IncomeType iType = ctx.IncomeTypes.FirstOrDefault(et => et.IncomeTypeName == itName && et.IncomeCategory.User_ID == userID); if (iType == null) { return(0); } ctx.IncomeTypes.Remove(iType); ctx.SaveChanges(); return(1); } }
internal static int DeleteExpenseCategory(string name, int userID) { using (var ctx = new BudgetContext()) { ExpenseCategory eCat = ctx.ExpenseCategories.FirstOrDefault(et => et.ExpenseCategoryName == name && et.User_ID == userID); if (eCat == null) { return(0); } ctx.ExpenseCategories.Remove(eCat); ctx.SaveChanges(); return(1); } }
internal static int DeleteIncomeCategory(string name, int userID) { using (var ctx = new BudgetContext()) { IncomeCategory iCat = ctx.IncomeCategories.FirstOrDefault(it => it.IncomeCategoryName == name && it.User_ID == userID); if (iCat == null) { return(0); } ctx.IncomeCategories.Remove(iCat); ctx.SaveChanges(); return(1); } }
/// <summary> /// Add user Type /// </summary> /// <param name="str"></param> ///<returns>returns 1 if success, 0 if fail because UserType already exists</returns> internal static int AddUserType(string str) { using (var ctx = new BudgetContext()) { UserType type = ctx.UserTypes.FirstOrDefault(u => u.UserTypeName == str); if (type == null) { ctx.UserTypes.Add(new UserType() { UserTypeName = str }); ctx.SaveChanges(); return(1); } return(0); } }
/// <summary> /// /// </summary> /// <param name="employerID"></param> /// <param name="employeeID"></param> /// <returns>returns 1 if success, 0 if fail because usertypes are not correct</returns> internal static int AddEmployee(int employerID, int employeeID) { using (var ctx = new BudgetContext()) { User Employer = ctx.Users.Find(employerID); User Employee = ctx.Users.Find(employeeID); if (Employer.UserType.UserTypeName == "Corporate" && Employee.UserType.UserTypeName != "Corporate") { Employee.Employer = Employer; ctx.SaveChanges(); return(1); } else { return(0); } } }
/// <summary> /// Add user /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <param name="userType"></param> ///<returns>returns userID if success, 0 if fail because User already exists</returns> internal static int AddUser(string username, string password, string userType) { using (var ctx = new BudgetContext()) { User user = ctx.Users.FirstOrDefault(u => u.UserName == username); UserType type = ctx.UserTypes.FirstOrDefault(u => u.UserTypeName == userType); if (user == null) { ctx.Users.Add(new User() { UserName = username, Password = password, UserType = type }); ctx.SaveChanges(); int userID = ctx.Users.FirstOrDefault(u => u.UserName == username).UserID; return(userID); } return(0); } }
/// <summary> /// Try to Add Expense Category with given name to user /// </summary> /// <param name="name"></param> /// <param name="userID"></param> ///<returns>returns 1 if success, 0 if fail because ExpenseCategory already exists</returns> internal static int AddExpenseCategory(string name, int userID) { string nameCaseCorrected = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(name.ToLower()); using (var ctx = new BudgetContext()) { ExpenseCategory eCat = ctx.ExpenseCategories.FirstOrDefault(ec => ec.ExpenseCategoryName == name && ec.User_ID == userID); if (eCat == null) { ctx.ExpenseCategories.Add(new ExpenseCategory() { ExpenseCategoryName = nameCaseCorrected, User_ID = userID }); ctx.SaveChanges(); return(1); } else { return(0); } } }
/// <summary> /// Adds income type to given category /// </summary> /// <param name="newETypeName"></param> /// <param name="userID"></param> /// <param name="cat">Category</param> ///<returns>returns 1 if success, 0 if fail because IncomeType already exists</returns> internal static int AddIncomeTypetoCategory(string newETypeName, int userID, string cat) { string nameCaseCorrected = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(newETypeName.ToLower()); using (var ctx = new BudgetContext()) { IncomeType iType = ctx.IncomeTypes.FirstOrDefault(it => it.IncomeTypeName == nameCaseCorrected && it.IncomeCategory.User_ID == userID); IncomeCategory iCat = ctx.IncomeCategories.FirstOrDefault(ic => ic.IncomeCategoryName == cat && ic.User_ID == userID); if (iType == null) { ctx.IncomeTypes.Add(new IncomeType() { IncomeTypeName = nameCaseCorrected, IncomeCategory = iCat }); ctx.SaveChanges(); return(1); } return(0); } }