/// <summary> /// Delete Payment from Database /// </summary> /// <param name="payment">Payment to Delete</param> /// <returns>Count of affected rows in database</returns> public static int Delete(Payment payment) { int c = 0; try { using (var db = new PaymentsContext()) { db.Payments.Attach(payment); db.Payments.Remove(payment); c = db.SaveChanges(); } if (c < 1) { throw new Exception("Delete<Payment>() failed to delete payment!"); } return(c); } catch (Exception ex) { throw ex; } }
/// <summary> /// Updates existing Payment in Database /// </summary> /// <param name="payment">Payment to Update</param> /// <returns>Count of affected rows in database</returns> public static int Update(Payment payment) { int c = 0; try { using (var db = new PaymentsContext()) { db.Payments.Attach(payment); db.Entry(payment).State = EntityState.Modified; c = db.SaveChanges(); } if (c < 1) { throw new Exception("Update<Payment>() failed to update payment's details!"); } return(c); } catch (Exception ex) { throw ex; } }
/// <summary> /// Delete Category with sub categories from Database /// </summary> /// <param name="category">Category to Delete</param> /// <returns>Count of affected rows in database</returns> public static int Delete(Category category) { int c = 0; try { using (var db = new PaymentsContext()) { var subCategories = db.Categories.Where(p => p.ParentId == category.Id); foreach (var subCategory in subCategories) { db.Categories.Attach(subCategory); db.Categories.Remove(subCategory); } db.Categories.Attach(category); db.Categories.Remove(category); c = db.SaveChanges(); } if (c < 1) { throw new Exception("Delete<Category>() failed to delete category!"); } return(c); } catch (Exception ex) { throw ex; } }
/// <summary> /// Updates existing Category in Database /// </summary> /// <param name="category">Category to Update</param> /// <returns>Count of affected rows in database</returns> public static int Update(Category category) { int c = 0; try { using (var db = new PaymentsContext()) { db.Categories.Attach(category); db.Entry(category).State = EntityState.Modified; c = db.SaveChanges(); } if (c < 1) { throw new Exception("Update<Category>() failed to update category's details!"); } return(c); } catch (Exception ex) { throw ex; } }
/// <summary> /// Get Categories from Database /// </summary> /// <param name="loggedUser">Logged User</param> /// <param name="parentId">Parent Id (default Empty Guid)</param> /// <param name="includeArchived">If true archived users will be listed too (default false)</param> /// <returns>List of Categories</returns> public static List <Category> GetList(User loggedUser, Guid parentId = default(Guid), bool includeArchived = false) { List <Category> list; try { using (var db = new PaymentsContext()) { if (includeArchived) { list = db.Categories .Where(p => p.ParentId == parentId && (p.OwnerId == Guid.Empty || p.OwnerId == loggedUser.Id)) .OrderBy(p => p.Name) .ToList(); } else { list = db.Categories .Where(p => p.Archived == null && p.ParentId == parentId && (p.OwnerId == Guid.Empty || p.OwnerId == loggedUser.Id)) .OrderBy(p => p.Name) .ToList(); } } return(list); } catch (Exception ex) { throw ex; } }
/// <summary> /// Delete User from Database /// </summary> /// <param name="user">User to Delete</param> /// <returns>Count of affected rows in database</returns> public static int Delete(User user) { int c = 0; try { using (var db = new PaymentsContext()) { db.Users.Attach(user); db.Users.Remove(user); c = db.SaveChanges(); } if (c < 1) { throw new Exception("Delete<User>() failed to delete user!"); } return(c); } catch (Exception ex) { throw ex; } }
/// <summary> /// Get User by User Name /// </summary> /// <param name="userName">User Name</param> /// <returns>Matching User by User Name</returns> public static User GetUserByUserName(string userName) { User user; try { using (var db = new PaymentsContext()) { user = db.Users.FirstOrDefault(p => p.UserName == userName); } return(user); } catch (Exception ex) { throw ex; } }
/// <summary> /// Login User /// </summary> /// <param name="userName">User's user name or email</param> /// <param name="password">Password</param> /// <returns>User matching with User Name and Password</returns> public static User LoginUser(string userName, string password) { User user; try { string hashedPassword = CalculateHashedPassword(password); using (var db = new PaymentsContext()) { user = db.Users.FirstOrDefault(p => (p.UserName == userName || p.Email == userName) && p.Password == hashedPassword); } return(user); } catch (Exception ex) { throw ex; } }
/// <summary> /// Get List of Payments /// </summary> /// <param name="loggedUser">Logged User</param> /// <returns>List of Payments</returns> public static List <Payment> GetList(User loggedUser) { List <Payment> list; try { using (var db = new PaymentsContext()) { list = db.Payments .Where(p => p.OwnerId == loggedUser.Id) .OrderByDescending(p => p.PaidDate) .ToList(); } return(list); } catch (Exception ex) { throw ex; } }
/// <summary> /// Get Users from Database /// </summary> /// <param name="loggedUser">Logged User</param> /// <param name="includeArchived">If true archived users will be listed too (default false)</param> /// <returns>List of Users</returns> public static List <User> GetList(User loggedUser, bool includeArchived = false) { List <User> list; try { using (var db = new PaymentsContext()) { if (loggedUser.UserRole == UserRole.AdminUser) { if (includeArchived) { list = db.Users .OrderBy(p => p.FirstName) .ThenBy(p => p.LastName) .ToList(); } else { list = db.Users .Where(p => p.Archived == null) .OrderBy(p => p.FirstName) .ThenBy(p => p.LastName) .ToList(); } } else { list = db.Users.Where(p => p.Id == loggedUser.Id).ToList(); } } return(list); } catch (Exception ex) { throw ex; } }
/// <summary> /// Creates new Payment to Database /// </summary> /// <param name="payment">Payment to Create</param> /// <returns>Count of affected rows in database</returns> public static int Create(Payment payment) { int c = 0; try { using (var db = new PaymentsContext()) { db.Payments.Add(payment); c = db.SaveChanges(); } if (c < 1) { throw new Exception("Create<Payment>() failed to create new payment!"); } return(c); } catch (Exception ex) { throw ex; } }
/// <summary> /// Creates new Category to Database /// </summary> /// <param name="category">Category to Create</param> /// <returns>Count of affected rows in database</returns> public static int Create(Category category) { int c = 0; try { using (var db = new PaymentsContext()) { db.Categories.Add(category); c = db.SaveChanges(); } if (c < 1) { throw new Exception("Create<Category>() failed to create new category!"); } return(c); } catch (Exception ex) { throw ex; } }
/// <summary> /// Creates new User to Database /// </summary> /// <param name="user">User to Create</param> /// <returns>Count of affected rows in database</returns> public static int Create(User user) { int c = 0; try { using (var db = new PaymentsContext()) { db.Users.Add(user); c = db.SaveChanges(); } if (c < 1) { throw new Exception("Create<User>() failed to create new user!"); } return(c); } catch (Exception ex) { throw ex; } }