/// <summary> /// Allows creation of a customer in the database. /// </summary> /// <param name="inUser">Customer information</param> /// <returns>A empty string if successful, else a string containing an appropriate error message to the user.</returns> public string CreateUser(UserVM inUser) { try { var email = inUser.Email; bool emailFound = FindEmail(email); if (emailFound == true) { return("Epostadressen du har oppgitt finnes allerede i databasen."); } byte[] salt = CreateSalt(); byte[] passWord = CreateHashedPassword(inUser.PassWord, salt); using (var db = new VideoDB()) { var newUserReg = new User { FirstName = inUser.FirstName, SurName = inUser.SurName, Address = inUser.Address, PhoneNr = inUser.PhoneNr, Email = inUser.Email, Admin = inUser.Admin, Salt = salt, PassWord = passWord }; PostalAddress findPostPlace = db.PostalAddresses.Find(inUser.PostalNr); if (findPostPlace == null) { var newPostal = new PostalAddress { PostalCode = inUser.PostalNr, Postal = inUser.Postal }; newUserReg.PostalAddress = newPostal; } else { newUserReg.PostalAddress = db.PostalAddresses.Find(inUser.PostalNr); } db.Users.Add(newUserReg); db.SaveChanges(); return(""); } } catch (Exception e) { new ErrorLogRepository().CreateError("Oblig1.DAL.UserDAL.CreateUser(UserVM inUser)", inUser.ToString(), e); return("Kunne ikke lagre kunden i databasen, vennligst kontakt kundeservice!"); } }
/// <summary> /// Resets password with new values in UserVM /// </summary> /// <param name="user"></param> /// <returns></returns> public bool ResetPassWord(UserVM user) { try { using (var db = new VideoDB()) { byte[] salt = CreateSalt(); byte[] password = CreateHashedPassword(user.PassWord, salt); var cDB = db.Users.FirstOrDefault(c => c.Email == user.Email); cDB.Salt = salt; cDB.PassWord = password; db.SaveChanges(); return(true); } } catch (Exception e) { new ErrorLogRepository().CreateError("Oblig1.DAL.UserDAL.ResetPassWord(userVM User)", user.ToString(), e); return(false); } }
/// <summary> /// Creates an order, based on a film ID list and a user. /// </summary> /// <param name="customerVM">The customer to create the order for.</param> /// <param name="selectedFilmIdList">The selected film id list to base the order on.</param> /// <returns>True if the order was created successfully, else false.</returns> public bool CreateOrder(UserVM customerVM, List <int> selectedFilmIdList) { try { using (var db = new VideoDB()) { User customer = db.Users.FirstOrDefault(k => k.Email == customerVM.Email); Order order = new Order() { User = customer, Date = DateTime.Now }; List <OrderLine> orderLine = new List <OrderLine>(); for (int i = 0; i < selectedFilmIdList.Count; i++) { Film film = db.Films.Find(selectedFilmIdList[i]); orderLine.Add(new OrderLine() { Order = order, Film = film, Price = db.PriceClasses.Find(film.PriceClassId).Price }); } db.OrderLines.AddRange(orderLine); db.Orders.Add(order); db.SaveChanges(); return(true); } } catch (Exception e) { new ErrorLogRepository().CreateError("Oblig1.DAL.OrderRepository.CreateOrder(CustomerVM customerVM, List<int> selectedFilmIdList)", customerVM.ToString() + "\r\n" + selectedFilmIdList.ToString(), e); return(false); } }