/// <summary> /// Adds a new role to the user /// </summary> /// <param name="email">The user's email</param> /// <param name="roleName">The role to add to the user</param> /// <returns>Return true on successful operation, else a string with an error message</returns> public static UserRoleManagerResult AddRoleToUser(string email, string roleName) { UserRoleManagerResult userManagerResult = new UserRoleManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", string.Empty) == email) { Role roleToAdd = Enumerable.FirstOrDefault(alenMotorsDbEntities.Roles, role => role.RoleName.Replace(" ", string.Empty) == roleName); UserRoleManagerResult userInRoles = GetUserRoles(email); if (userInRoles.Roles.Any(userInRole => userInRole == roleName)) { userManagerResult.ErrorMessage = "User already in role"; return userManagerResult; } account.AccountsInRoles.Add(new AccountInRole {AccountID = account.AccountID, RoleID = roleToAdd.RoleID}); alenMotorsDbEntities.SaveChanges(); userManagerResult.Success = true; return userManagerResult; } } } return null; } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } }
/// <summary> /// Edit an existing branch /// </summary> /// <param name="branchName"></param> /// <param name="branchNewName"></param> /// <returns>Retuns true if the addition has Succeeded, else returns string with an error message</returns> public static BranchManagerResult EditBranch(string branchName, string branchNewName) { BranchManagerResult branchManagerResult = new BranchManagerResult(); using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Branch branch in alenMotorsDbEntities.Branches.ToList().Where(branch => branch.BranchName == branchName)) { branch.BranchName = branchNewName; branchManagerResult.Success = true; return branchManagerResult; } } return null; }
/// <summary> /// Gets all list of all Vehicles /// </summary> /// <returns>Returlss true along with a List<Vehicle> else will retunr a sting with the error message </returns> public static VehicleManagerResult GetVehicles() { VehicleManagerResult vehicleManagerResult = new VehicleManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Vehicle vehicle in alenMotorsDbEntities.Vehicles.ToList()) { vehicleManagerResult.VehicleList.Add(vehicle); } vehicleManagerResult.Success = true; return vehicleManagerResult; } } catch (Exception ex) { vehicleManagerResult.ErrorMessage = ex.Message; return vehicleManagerResult; } }
public static SearchManagerResult ViewOrdersContainsSearch(string searchStr) { SearchManagerResult searchManagerResult = new SearchManagerResult(); List <Order> searchResult = new List <Order>(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Order order in alenMotorsDbEntities.Orders.ToList()) { if (order.Account.Email.Replace(" ", String.Empty).IndexOf(searchStr, StringComparison.CurrentCultureIgnoreCase) != -1) { searchResult.Add(order); } if (order.Vehicle.Color.Replace(" ", String.Empty).IndexOf(searchStr, StringComparison.CurrentCultureIgnoreCase) != -1) { searchResult.Add(order); } if (order.Vehicle.Manufacturer.Replace(" ", String.Empty).IndexOf(searchStr, StringComparison.CurrentCultureIgnoreCase) != -1) { searchResult.Add(order); } if (order.Vehicle.Model.Replace(" ", String.Empty).IndexOf(searchStr, StringComparison.CurrentCultureIgnoreCase) != -1) { searchResult.Add(order); } if ( order.Vehicle.Branch.BranchName.Replace(" ", String.Empty).IndexOf(searchStr, StringComparison.CurrentCultureIgnoreCase) != -1) { searchResult.Add(order); } if (order.Vehicle.Status.Replace(" ", String.Empty).IndexOf(searchStr, StringComparison.CurrentCultureIgnoreCase) != -1) { searchResult.Add(order); } if (searchStr.IndexOf("Available", StringComparison.CurrentCultureIgnoreCase) != -1) { if (order.Status) { searchResult.Add(order); } } if (searchStr.IndexOf("Not Available", StringComparison.CurrentCultureIgnoreCase) != -1) { if (order.Status) { searchResult.Add(order); } } } searchManagerResult.Orders = searchResult.Distinct().ToList(); searchManagerResult.Success = true; return searchManagerResult; } } catch (Exception ex) { searchManagerResult.ErrorMessage = ex.Message; return searchManagerResult; } }
public static SearchManagerResult ViewOrdersOrderIdGreaterThanSearch(string searchStr) { SearchManagerResult searchManagerResult = new SearchManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Order order in alenMotorsDbEntities.Orders.ToList()) { if (order.OrderID > int.Parse(searchStr)) { searchManagerResult.Orders.Add(order); } } searchManagerResult.Success = true; return searchManagerResult; } } catch (Exception ex) { searchManagerResult.ErrorMessage = ex.Message; return searchManagerResult; } }
/// <summary> /// Remove an existing branch /// </summary> /// <param name="branchName"></param> /// <returns>Retuns true if the addition has Succeeded, else returns string with an error message</returns> public static BranchManagerResult RemoveBranch(string branchName) { BranchManagerResult branchManagerResult = new BranchManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Branch branch in alenMotorsDbEntities.Branches.ToList().Where(branch => branch.BranchName.Replace(" ", String.Empty) == branchName)) { alenMotorsDbEntities.Branches.Remove(branch); alenMotorsDbEntities.SaveChanges(); branchManagerResult.Success = true; return branchManagerResult; } } } catch (Exception ex) { branchManagerResult.ErrorMessage = ex.Message; return branchManagerResult; } return null; }
/// <summary> /// Verifies the account /// </summary> /// <param name="email"></param> /// <param name="verifyString"></param> /// <returns>Retuns true if the verification was correct, else returns string with an error message</returns> public static UserManagerResult VerifyUser(string email, string verifyString) { UserManagerResult userManagerResult = new UserManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", String.Empty) == email && account.VerifyString.Replace(" ", String.Empty) == verifyString) { account.Verified = true; alenMotorsDbEntities.SaveChanges(); userManagerResult.Success = true; return userManagerResult; } } } } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } return null; }
/// <summary> /// Gets the orders for the following email /// </summary> /// <param name="email"></param> /// <returns>Retuns true if the operation has Succeeded, else returns string with an error message</returns> public static OrderManagerResult GetOrders(string email) { OrderManagerResult orderManagerResult = new OrderManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", String.Empty) == email) { foreach (Order order in account.Orders.ToList()) { orderManagerResult.Orders.Add(order); } orderManagerResult.Success = true; return orderManagerResult; } } } } catch (Exception ex) { orderManagerResult.ErrorMessage = ex.Message; return orderManagerResult; } return null; }
/// <summary> /// Add a new Vehicle /// </summary> /// <param name="newVehicle"> New Vehicle class</param> /// <param name="imageFile"> Vehicle Image</param> /// <param name="serverPath"> Serer path used used to save the image</param> /// <returns>Retuns true if the addition has Succeeded, else returns string with an error message</returns> public static VehicleManagerResult AddNewVehicle(Vehicle newVehicle, HttpPostedFileBase imageFile, string serverPath) { VehicleManagerResult vehicleManagerResult = new VehicleManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { string[] allowedExtensions = new[] {".Jpg", ".png", ".jpg", ".jpeg", ".gif"}; string fileExt = Path.GetExtension(imageFile.FileName); // Get file Extension // Check if file in the right extension if (!allowedExtensions.Contains(fileExt)) { vehicleManagerResult.ErrorMessage = "Wrong file Format"; return vehicleManagerResult; } foreach (Branch branch in alenMotorsDbEntities.Branches.ToList()) { if (branch.BranchName.Replace(" ", String.Empty) == newVehicle.Branch.BranchName) { newVehicle.Branch = branch; } } string fileName = Path.GetFileName(imageFile.FileName); // Get only file name string path = System.IO.Path.Combine(serverPath, fileName); // Path to where the image will be saved to newVehicle.ImageUrl = fileName; if (alenMotorsDbEntities.Vehicles.ToList().Any(vehicle => vehicle.LicensePlate == newVehicle.LicensePlate)) { vehicleManagerResult.ErrorMessage = "This license plate already exists"; return vehicleManagerResult; } Image newImage = Image.FromStream(imageFile.InputStream, true, true); Image resizedImage = Generic.ResizeImage(newImage, 400, 250); resizedImage.Save(path); alenMotorsDbEntities.Vehicles.Add(newVehicle); alenMotorsDbEntities.SaveChanges(); vehicleManagerResult.Success = true; return vehicleManagerResult; } } catch (Exception ex) { vehicleManagerResult.ErrorMessage = ex.Message; return vehicleManagerResult; } }
/// <summary> /// Add a new Order /// </summary> /// <param name="email">Email</param> /// <param name="vehicleID">vehicleID can be null</param> /// <param name="startDate">startDate</param> /// <param name="endDate">endDate</param> /// <param name="rentDays">rentDays</param> /// <returns>Retuns true if the addition has Succeeded, else returns string with an error message</returns> public static OrderManagerResult AddOrder(string email, string vehicleID, string startDate, string endDate, string rentDays) { OrderManagerResult orderManagerResult = new OrderManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Vehicle vehicle in alenMotorsDbEntities.Vehicles.ToList()) { if (vehicle.VehicleID == int.Parse(vehicleID)) { if (vehicle.Status == "Not Avalible") { orderManagerResult.ErrorMessage = "This Vehicle isn't available right now"; return orderManagerResult; } // Vehicle = vehicle => maybe instead would've been better to use ID Order newOrder = new Order { StartDate = startDate, EndDate = endDate, Vehicle = vehicle, Status = false, RentDays = int.Parse(rentDays) }; foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", String.Empty) == email) { account.Orders.Add(newOrder); //alenMotorsDbEntities.Orders.Add(newOrder); vehicle.Status = "Not Available"; alenMotorsDbEntities.SaveChanges(); orderManagerResult.Success = true; return orderManagerResult; } } } } } } catch (Exception ex) { orderManagerResult.ErrorMessage = ex.Message; return orderManagerResult; } orderManagerResult.ErrorMessage = "Something went wrong"; return orderManagerResult; }
/// <summary> /// Add a new user /// </summary> /// <param name="email"></param> /// <param name="firstName"></param> /// <param name="lastName"></param> /// <param name="password"></param> /// <param name="gender"></param> /// <param name="phoneNumber"></param> /// <param name="birthDate"></param> /// <returns>Retuns true if the addition has Succeeded, else returns string with an error message</returns> public static UserManagerResult AddUser(string email, string firstName, string lastName, string password, string gender, int phoneNumber, string birthDate) { UserManagerResult userManagerResult = new UserManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { string verifyString = Generic.VerifyEmailHash(email); if (alenMotorsDbEntities.Accounts.Any(account => account.Email == email)) { userManagerResult.ErrorMessage = "Email is already in use, please use a different one."; return userManagerResult; } alenMotorsDbEntities.Accounts.Add(new Account { Email = email, FirstName = firstName, LastName = lastName, Password = Generic.EncodePassword(password, email), Gender = gender, BirthDate = birthDate, PhoneNumber = phoneNumber, RegistrationDate = DateTime.Now.ToString("{0:d/M/yyyy HH:mm:ss}"), VerifyString = verifyString }); alenMotorsDbEntities.SaveChanges(); UserRoleManagerResult addUserToRole = UserRoleManager.AddRoleToUser(email, "User"); if (addUserToRole.Success) { // userManagerResult.VerifyString = verifyString; userManagerResult.Success = true; return userManagerResult; } userManagerResult.ErrorMessage = "Some thing went wrong"; return userManagerResult; } } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } }
public static UserRoleManagerResult RemoveRole(string roleName) { UserRoleManagerResult userManagerResult = new UserRoleManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { int roleIdToRemove; foreach (Role role in alenMotorsDbEntities.Roles.ToList().Where(role => role.RoleName.Replace(" ", String.Empty) == roleName)) { roleIdToRemove = role.RoleID; foreach (AccountInRole accountInRole in alenMotorsDbEntities.AccountInRoles.ToList().Where(accountInRolein => accountInRolein.RoleID == roleIdToRemove)) { alenMotorsDbEntities.AccountInRoles.Remove(accountInRole); } alenMotorsDbEntities.SaveChanges(); userManagerResult.Success = true; return userManagerResult; } } } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } return userManagerResult; }
/// <summary> /// Add a new role to the avalaible roles /// </summary> /// <param name="roleNameNew">Name of the new role</param> /// <returns>Return true on successful operation, else a string with the error message</returns> public static UserRoleManagerResult AddRole(string roleNameNew) { UserRoleManagerResult userManagerResult = new UserRoleManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { UserRoleManagerResult checkRoles = GetRoles(); // Check if the role already exists, case senstive. Example User = user will return as the role already exists if ( checkRoles.Roles.Select(roleName => roleName.Equals(roleNameNew, StringComparison.CurrentCultureIgnoreCase)). Any(stringEqual => stringEqual)) { userManagerResult.ErrorMessage = "This Role already exists"; return userManagerResult; } alenMotorsDbEntities.Roles.Add(new Role {RoleName = roleNameNew}); alenMotorsDbEntities.SaveChanges(); } userManagerResult.Success = true; return userManagerResult; } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } }
/// <summary> /// Add new Branch (string) /// </summary> /// <param name="branchName">The name of the new branch</param> /// <returns>Retuns true if the addition has Succeeded, else returns string with an error message</returns> public static BranchManagerResult AddBranch(string branchName) { BranchManagerResult branchManagerResult = new BranchManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { List <Branch> branchList = alenMotorsDbEntities.Branches.ToList(); if ( branchList.Any( branch => branchName.Equals(branch.BranchName.Replace(" ", String.Empty), StringComparison.CurrentCultureIgnoreCase))) { branchManagerResult.ErrorMessage = "A branch with this name already exists"; return branchManagerResult; } Branch addBranch = new Branch {BranchName = branchName}; alenMotorsDbEntities.Branches.Add(addBranch); alenMotorsDbEntities.SaveChanges(); branchManagerResult.Success = true; return branchManagerResult; } } catch (Exception ex) { branchManagerResult.ErrorMessage = ex.Message; return branchManagerResult; } }
/// <summary> /// Get all roles names List<string> /// </summary> /// <returns>Returns a List<string> of all roles names, else a string with an error message </string></returns> public static UserRoleManagerResult GetRoles() { UserRoleManagerResult userManagerResult = new UserRoleManagerResult(); List <string> roles = new List <string>(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { roles.AddRange(alenMotorsDbEntities.Roles.ToList().Select(role => role.RoleName.Replace(" ", string.Empty))); } userManagerResult.Success = true; userManagerResult.Roles = roles; return userManagerResult; } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } }
/// <summary> /// Gets all the Roles which the user belongs to, retunrs string[] /// </summary> /// <param name="email">The users mail</param> /// <returns>Returns all the names of roles which the user belongs to (string[]), else a string with an error message</returns> public static UserRoleManagerResult GetUserRoles(string email) { UserRoleManagerResult userManagerResult = new UserRoleManagerResult(); List <Role> roles = new List <Role>(); List <string> userRoles = new List <string>(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { roles.AddRange(alenMotorsDbEntities.Roles); foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", string.Empty) == email) { foreach (AccountInRole accountInRole in account.AccountsInRoles.ToList()) { userRoles.AddRange(from role in roles where role.RoleID == accountInRole.RoleID select role.RoleName.Replace(" ", string.Empty)); } userManagerResult.Roles = userRoles; userManagerResult.Success = true; return userManagerResult; } } return null; } } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } }
/// <summary> /// Removes a role from the user /// </summary> /// <param name="email">The user's email</param> /// <param name="roleName">The role to remove from the user</param> /// <returns>Return true on successful operation, else a string with an error message</returns> public static UserRoleManagerResult RemoveRoleFromUser(string email, string roleName) { UserRoleManagerResult userManagerResult = new UserRoleManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", string.Empty) == email) { foreach (Role role in alenMotorsDbEntities.Roles.ToList()) { if (role.RoleName.Replace(" ", String.Empty) == roleName) { int roleIDtoRemove = role.RoleID; if (roleIDtoRemove == 4) { userManagerResult.ErrorMessage = "You can't remove the Role User"; return userManagerResult; } AccountInRole accountInRoleToRemove = new AccountInRole { AccountID = account.AccountID, RoleID = roleIDtoRemove }; alenMotorsDbEntities.AccountInRoles.Attach(accountInRoleToRemove); alenMotorsDbEntities.AccountInRoles.Remove(accountInRoleToRemove); alenMotorsDbEntities.SaveChanges(); userManagerResult.Success = true; return userManagerResult; } } } } } userManagerResult.ErrorMessage = "Mis match"; return userManagerResult; } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } }
/// <summary> /// Get all branches names into (string) /// </summary> /// <returns>Returns a List<string> of all branches names, else a string with an error message </string></returns> public static BranchManagerResult GetBranchesNames() { BranchManagerResult branchManagerResult = new BranchManagerResult(); List <string> branches = new List <string>(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { branches.AddRange(alenMotorsDbEntities.Branches.ToList().Select(branch => branch.BranchName.Replace(" ", String.Empty))); } branchManagerResult.Success = true; branchManagerResult.BranchesNames = branches; return branchManagerResult; } catch (Exception ex) { branchManagerResult.ErrorMessage = ex.Message; return branchManagerResult; } }
/// <summary> /// Removes a Vehicle based on ID /// </summary> /// <param name="vehicleID"></param> /// <param name="serverPath"></param> /// <returns>Returns true if the operation was successful else will retrun a string with the error message</returns> public static VehicleManagerResult RemoveVehicle(string vehicleID, string serverPath) { VehicleManagerResult vehicleManagerResult = new VehicleManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Vehicle vehicle in alenMotorsDbEntities.Vehicles.ToList()) { if (vehicle.VehicleID == int.Parse(vehicleID)) { string path = Path.Combine(serverPath, vehicle.ImageUrl); File.Delete(path); alenMotorsDbEntities.Vehicles.Remove(vehicle); alenMotorsDbEntities.SaveChanges(); vehicleManagerResult.Success = true; return vehicleManagerResult; } } } } catch (Exception ex) { vehicleManagerResult.ErrorMessage = ex.Message; return vehicleManagerResult; } return null; }
/// <summary> /// Returns the infrmation that corresponds to the provided email /// </summary> /// <param name="email">Email</param> /// <returns>Returns all the corresponds information (Account object), else a string with an error message</returns> public static UserManagerResult GetUser(string email) { UserManagerResult userManagerResult = new UserManagerResult(); Account user = new Account(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Account account in alenMotorsDbEntities.Accounts) { if (account.Email.Replace(" ", string.Empty) != email) { continue; } user.Email = account.Email; user.LastName = account.LastName; user.FirstName = account.FirstName; user.Gender = account.Gender; user.PhoneNumber = account.PhoneNumber; user.BirthDate = account.BirthDate; userManagerResult.User = user; userManagerResult.Success = true; return userManagerResult; } } } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } return userManagerResult; }
public static BranchManagerResult GetBrances() { BranchManagerResult branchManagerResult = new BranchManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Branch branch in alenMotorsDbEntities.Branches.ToList()) { branchManagerResult.Branches.Add(branch); } branchManagerResult.Success = true; return branchManagerResult; } } catch (Exception ex) { branchManagerResult.ErrorMessage = ex.Message; return branchManagerResult; } }
/// <summary> /// Removes an existing user /// </summary> /// <param name="email">The serach parameter at which to remove the user</param> /// <returns>Retuns true if the removal has Succeeded, else returns string with an error message</returns> public static UserManagerResult RemoveUser(string email) { UserManagerResult userManagerResult = new UserManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach ( Account account in alenMotorsDbEntities.Accounts.ToList().Where(account => account.Email.Replace(" ", String.Empty) == email)) { foreach (AccountInRole accountInRole in account.AccountsInRoles.ToList()) { account.AccountsInRoles.Remove(accountInRole); } foreach (Order order in account.Orders) { account.Orders.Remove(order); } alenMotorsDbEntities.Accounts.Remove(account); alenMotorsDbEntities.SaveChanges(); userManagerResult.Success = true; return userManagerResult; } return userManagerResult; } } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } }
/// <summary> /// Gets the branch name based on branch ID /// </summary> /// <param name="branchID">BranchID</param> /// <returns>Retuns the branch name which is coresponding to the ID</returns> public static BranchManagerResult GetBranch(int branchID) { BranchManagerResult branchManagerResult = new BranchManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Branch branch in alenMotorsDbEntities.Branches.ToList().Where(branch => branch.BranchID == branchID)) { branchManagerResult.Branch = branch; branchManagerResult.Success = true; return branchManagerResult; } } } catch (Exception ex) { branchManagerResult.ErrorMessage = ex.Message; return branchManagerResult; } return null; }
/// <summary> /// Get the specifc Vehicle based on ID /// </summary> /// <param name="vehicleIdStr"></param> /// <returns>Returns true with the object vehicle, else will retunr a sting with the error message</returns> public static VehicleManagerResult GetVehicle(string vehicleIdStr) { VehicleManagerResult vehicleManagerResult = new VehicleManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { int vehicleId = Int32.Parse(vehicleIdStr); foreach (Vehicle vehicle in alenMotorsDbEntities.Vehicles.ToList().Where(vehicle => vehicleId == vehicle.VehicleID)) { vehicleManagerResult.Vehicle = vehicle; vehicleManagerResult.Success = true; return vehicleManagerResult; } } } catch (Exception ex) { vehicleManagerResult.ErrorMessage = ex.Message; return vehicleManagerResult; } return null; }
/// <summary> /// Validates if the provided credentials are correct /// </summary> /// <param name="email">Email</param> /// <param name="password">Password</param> /// <returns>Return true on successful validation, else a string with an error message</returns> public static UserManagerResult Login(string email, string password) { UserManagerResult userManagerResult = new UserManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", String.Empty) == email) { if (account.Verified) { if (account.Password.Replace(" ", string.Empty) == Generic.EncodePassword(password, email)) { userManagerResult.Success = true; return userManagerResult; } userManagerResult.ErrorMessage = "Email and Password don't match."; return userManagerResult; } userManagerResult.ErrorMessage = "You haven't verified your account, please check your email and verify your account"; return userManagerResult; } } } } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } return null; }
/// <summary> /// Returns the list of all users /// </summary> /// <returns>Return true on successful action as well as List<Account> of all users, else a string with an error message</returns> public static UserManagerResult GetUsers() { UserManagerResult userManagerResult = new UserManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { userManagerResult.Success = true; userManagerResult.UserList.Add(account); } return userManagerResult; } } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } }
/// <summary> /// Edits a vehicle based on the passed information /// </summary> /// <param name="vehicleToEdit"></param> /// <param name="imageFile"></param> /// <param name="serverPath"></param> /// <returns>Returns true if the operation was successful else will return a stirng with the error message</returns> public static VehicleManagerResult EditVehicle(Vehicle vehicleToEdit, HttpPostedFileBase imageFile, string serverPath) { VehicleManagerResult vehicleManagerResult = new VehicleManagerResult(); try { string path = null; string oldPath = null; // with Image update if (imageFile != null) { string[] allowedExtensions = new[] {".Jpg", ".png", ".jpg", ".jpeg", ".gif"}; string fileExt = Path.GetExtension(imageFile.FileName); // Get file Extension // Check if file in the right extension if (!allowedExtensions.Contains(fileExt)) { vehicleManagerResult.ErrorMessage = "Wrong file Format"; return vehicleManagerResult; } path = Path.Combine(serverPath, imageFile.FileName); // Path to where the image will be saved to } // without Image update using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Vehicle vehicle in alenMotorsDbEntities.Vehicles.ToList()) { if (vehicle.VehicleID == vehicleToEdit.VehicleID) { vehicle.DistanceTraveled = vehicleToEdit.DistanceTraveled; vehicle.Color = vehicleToEdit.Color; vehicle.ManufacturYear = vehicleToEdit.ManufacturYear; vehicle.Manufacturer = vehicleToEdit.Manufacturer; vehicle.LicensePlate = vehicleToEdit.LicensePlate; vehicle.Price = vehicleToEdit.Price; if (vehicleToEdit.Transmission != null) { vehicle.Transmission = vehicleToEdit.Transmission; } if (vehicleToEdit.Status != null) { vehicle.Status = vehicleToEdit.Status; } if (vehicleToEdit.Branch.BranchName != null) { foreach (Branch branch in alenMotorsDbEntities.Branches.ToList()) { if (branch.BranchName.Replace(" ", String.Empty) == vehicleToEdit.Branch.BranchName) { vehicle.Branch = branch; vehicle.BranchID = branch.BranchID; } } } if (path != null) { Image newImage = Image.FromStream(imageFile.InputStream, true, true); Image resizedImage = Generic.ResizeImage(newImage, 400, 250); resizedImage.Save(path); //newImage.Save(path); //Generic.FormatPicture(imageFile, path, 400,400); //imageFile.SaveAs(path); oldPath = Path.Combine(serverPath, vehicle.ImageUrl); if (File.Exists(oldPath)) { File.Delete(oldPath); vehicle.ImageUrl = imageFile.FileName; } } alenMotorsDbEntities.SaveChanges(); vehicleManagerResult.Success = true; return vehicleManagerResult; } } } } catch (Exception ex) { vehicleManagerResult.ErrorMessage = ex.Message; return vehicleManagerResult; } return null; }
public static VehicleManagerResult GetVehicleById(int id) { VehicleManagerResult vehicleManagerResult = new VehicleManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Vehicle vehicle in alenMotorsDbEntities.Vehicles.ToList()) { if (vehicle.VehicleID == id) { vehicleManagerResult.Vehicle = vehicle; vehicleManagerResult.Success = true; return vehicleManagerResult; } } } } catch (Exception ex) { vehicleManagerResult.ErrorMessage = ex.Message; return vehicleManagerResult; } return null; }
public static UserManagerResult GetUserByOrderID(int orderID) { UserManagerResult userManagerResult = new UserManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Orders.ToList().Any(Order => Order.OrderID == orderID)) { userManagerResult.User = account; userManagerResult.Success = true; return userManagerResult; } } } } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } return null; }
/// <summary> /// Update user /// </summary> /// <param name="email">Email</param> /// <param name="userAccount">User account [Account]</param> /// <param name="additionalinformation">New password</param> /// <returns>Return true on successful update, else a string with an error message</returns> public static UserManagerResult EditUser(string email, Account userAccount, string additionalinformation) { UserManagerResult userManagerResult = new UserManagerResult(); try { using (AlenMotorsDbEntities alenMotorsDbEntities = new AlenMotorsDbEntities()) { // Allternative 1 if (email != userAccount.Email) { foreach (Account account0 in alenMotorsDbEntities.Accounts.ToList()) { if (account0.Email.Replace(" ", string.Empty) == email && account0.Password.Replace(" ", string.Empty) == Generic.EncodePassword(additionalinformation, email)) { foreach (Account account1 in alenMotorsDbEntities.Accounts.ToList()) { if (userAccount.Email == account1.Email.Replace(" ", String.Empty)) { if (userAccount.RegistrationDate != null) { account1.Email = userAccount.RegistrationDate; } account1.LastName = userAccount.LastName; account1.FirstName = userAccount.FirstName; account1.BirthDate = userAccount.BirthDate; account1.Gender = userAccount.Gender; account1.PhoneNumber = userAccount.PhoneNumber; if (userAccount.Password != null) { account1.Password = Generic.EncodePassword(userAccount.Password, userAccount.Email); } userManagerResult.Success = true; alenMotorsDbEntities.SaveChanges(); return userManagerResult; } } } } userManagerResult.Success = false; return userManagerResult; } // Allternative 2 if (userAccount.Password != null) { foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", string.Empty) == email && account.Password.Replace(" ", string.Empty) == Generic.EncodePassword(userAccount.Password, email)) { account.LastName = userAccount.LastName; account.FirstName = userAccount.FirstName; account.BirthDate = userAccount.BirthDate; account.Gender = userAccount.Gender; account.PhoneNumber = userAccount.PhoneNumber; if (additionalinformation != null) { account.Password = Generic.EncodePassword(additionalinformation, email); } if (email != account.Email.Replace(" ", String.Empty)) { account.Email = email; } userManagerResult.Success = true; alenMotorsDbEntities.SaveChanges(); return userManagerResult; } } } // Allternative 3 foreach (Account account in alenMotorsDbEntities.Accounts.ToList()) { if (account.Email.Replace(" ", string.Empty) == email && account.Password.Replace(" ", string.Empty) == Generic.EncodePassword(additionalinformation, email)) { account.LastName = userAccount.LastName; account.FirstName = userAccount.FirstName; account.BirthDate = userAccount.BirthDate; account.Gender = userAccount.Gender; account.PhoneNumber = userAccount.PhoneNumber; if (additionalinformation != null) { account.Password = Generic.EncodePassword(additionalinformation, email); } if (email != account.Email.Replace(" ", String.Empty)) { account.Email = email; } userManagerResult.Success = true; alenMotorsDbEntities.SaveChanges(); return userManagerResult; } } } userManagerResult.Success = false; return userManagerResult; } catch (Exception ex) { userManagerResult.ErrorMessage = ex.Message; return userManagerResult; } }