public User GetUser(string email)
 {
     using (var db = new VideoDB())
     {
         return(db.Users.FirstOrDefault(k => k.Email == email));
     }
 }
 /// <summary>
 /// Searches for films in the database that starts with the searchString, then adds them to a list.
 /// </summary>
 /// <param name="searchString">The string to search for in the database</param>
 /// <param name="sort">The sorting of the films</param>
 /// <seealso cref="ConvertFilm(Film)"/>
 /// <returns>En liste med filmVM </returns>
 public List <FilmVM> SearchByTitle(string searchString, Sort sort = Sort.alfa)
 {
     try
     {
         var allFilmsWithString = new List <FilmVM>();
         using (var db = new VideoDB())
         {
             foreach (var film in db.Films.ToList())
             {
                 if (film.Title.ToLower().StartsWith(searchString.ToLower()))
                 {
                     allFilmsWithString.Add(ConvertFilm(film));
                 }
                 // Adds films that has a title that starts with "the" when the searchstring does not
                 else if (film.Title.ToLower().StartsWith("the") && !searchString.ToLower().StartsWith("the"))
                 {
                     if (film.Title.ToLower().Substring(4).StartsWith(searchString.ToLower()))
                     {
                         allFilmsWithString.Add(ConvertFilm(film));
                     }
                 }
             }
         }
         return(allFilmsWithString);
     }
     catch (Exception e)
     {
         new ErrorLogRepository().CreateError("Oblig1.DAL.FilmDAL.SearchByTitle(string searchString, Sorter sort = Sorter.alfa)", searchString, e);
         return(new List <FilmVM>());
     }
 }
        /// <summary>
        /// Refunds a order, by setting it inactive in the database.
        /// </summary>
        /// <param name="ordernr">Order nr to refund.</param>
        /// <returns>True if succedded, else false.</returns>
        public bool RefundOrder(int ordernr)
        {
            try
            {
                using (var db = new VideoDB())
                {
                    Order order = db.Orders.Find(ordernr);

                    List <OrderLine> orderLines = db.OrderLines.Where(o => o.Order.OrderNr == ordernr).ToList();
                    foreach (var line in orderLines)
                    {
                        line.Active = false;
                    }

                    order.Active = false;

                    db.SaveChanges();
                    return(true);
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError(
                    "Oblig1.DAL.OrderRepository.RefundOrder(int ordernr)",
                    "Ordernr: " + ordernr,
                    e
                    );

                return(false);
            }
        }
        /// <summary>
        /// Grants editable User object.
        /// </summary>
        /// <param name="inUser"></param>
        /// <returns></returns>
        public bool EditUser(int id, EditUserVM inUser)
        {
            using (var db = new VideoDB())
            {
                try
                {
                    User user = db.Users.Find(id);
                    user.FirstName = inUser.FirstName;
                    user.SurName   = inUser.SurName;
                    user.Address   = inUser.Address;

                    PostalAddress findPostPlace = db.PostalAddresses.Find(inUser.PostalNr);

                    if (findPostPlace == null)
                    {
                        var newPostal = new PostalAddress
                        {
                            PostalCode = inUser.PostalNr,
                            Postal     = inUser.Postal
                        };
                        db.PostalAddresses.Add(newPostal);
                        user.PostalAddress = newPostal;
                    }
                    db.SaveChanges();
                    return(true);
                }
                catch (Exception e)
                {
                    new ErrorLogRepository().CreateError("Oblig1.DAL.UserDAL.EditUser(int id, UserVM inUser)", inUser.ToString() + " | id:" + id, e);
                    return(false);
                }
            }
        }
        /// <summary>
        /// Returns a helper class for creating new film objects by collecting
        /// the possible choices that is needed when selecting price class and genre(s).
        /// </summary>
        /// <returns>List of possible price classes to chose from</returns>
        public List <SelectListItem> GetPriceClassSelectList()
        {
            try
            {
                using (var db = new VideoDB())
                {
                    List <SelectListItem> PriceClassSelectList = db.PriceClasses.AsNoTracking()
                                                                 .OrderBy(n => n.Id)
                                                                 .Select(n => new SelectListItem
                    {
                        Value = n.Id.ToString(),
                        Text  = "Prisklasse " + n.Id + " (" + n.Price + " kr)"
                    }).ToList();
                    var listTop = new SelectListItem()
                    {
                        Value = null,
                        Text  = "--- Velg Prisklasse ---"
                    };
                    PriceClassSelectList.Insert(0, listTop);

                    return(PriceClassSelectList);
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.FilmDAL.GetPriceClassSelectList()", "PriceClassSelectList", e);
                return(null);
            }
        }
예제 #6
0
        /// <summary>
        /// Creates a errorlog item and saves it to  a new string line in ~/App_Data/Error.txt and to the database.
        /// </summary>
        /// <param name="message">Location of the error.</param>
        /// <param name="parameter">Parameters for the error instance.</param>
        /// <param name="e">Exception from the error.</param>
        /// <returns>If passed true.</returns>
        public bool CreateError(string message, string parameter, Exception e)
        {
            //If there is an error in the CreateError, then it should crash here.
            //Because errors are no longer logged, and the integrity is invalid.
            Console.Error.WriteLine(e.StackTrace);
            try
            {
                using (var db = new VideoDB())
                {
                    Error error = new Error
                    {
                        Message    = message,
                        Parameter  = parameter,
                        StackTrace = e.StackTrace
                    };

                    using (StreamWriter writetext = new StreamWriter(HostingEnvironment.MapPath("~/App_Data/Error.txt"), true))
                    {
                        writetext.WriteLine(error.ToString() + Environment.NewLine);
                    }

                    db.ErrorLogs.Add(error);
                    db.SaveChanges();
                }
            }
            catch (Exception)
            {
                throw new DatabaseErrorException(message);
            }
            throw new DatabaseErrorException(message);
        }
예제 #7
0
        protected void rptSpecial_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                var curData   = (DataRowView)e.Item.DataItem;
                var litPlayer = (Literal)e.Item.FindControl("litPlayer");

                var coverInfo = VideoDB.GetCover(ConvertUtility.ToInt32(curData["Content_ID"]));
                if (coverInfo != null)
                {
                    var type = coverInfo.Video_Type;

                    switch (type)
                    {
                    case "flash":
                        litPlayer.Text = MultimediaUtility.ShowFlashAdv("home", coverInfo.Video_File, 255, 158);
                        break;

                    case "youtube":
                        litPlayer.Text = MultimediaUtility.ShowYouTuBeAdv(coverInfo.Video_YouTube, 255, 158);
                        break;

                    default:
                        litPlayer.Text = MultimediaUtility.strInitMultimedia(coverInfo.Video_File, 255, 158);
                        break;
                    }
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Gets all error items in the database.
        /// </summary>
        /// <returns>A list of all the errors</returns>
        public List <ErrorLogVM> AllErrors()
        {
            try
            {
                using (var db = new VideoDB())
                {
                    List <ErrorLogVM> errorLogs = new List <ErrorLogVM>();
                    foreach (var log in db.ErrorLogs)
                    {
                        errorLogs.Add(new ErrorLogVM()
                        {
                            Id         = log.Id,
                            Message    = log.Message,
                            Parameter  = log.Parameter,
                            StackTrace = log.StackTrace,
                            Time       = TimeZoneInfo.ConvertTimeFromUtc(log.Time, tZone).ToString("dd.MM.yyyy HH:mm:ss")
                        });
                    }

                    return(errorLogs);
                }
            }
            catch (Exception e)
            {
                CreateError("Oblig1.DAL.Repositories.ErrorRepository.AllChanges", null, e);
                return(new List <ErrorLogVM>());
            }
        }
        /// <summary>
        /// Creates a list of all the films a customer owns.
        /// </summary>
        /// <param name="email">The customer e-mail</param>
        /// <returns>A list of the customer's films</returns>
        public List <FilmVM> GetUserFilms(string email)
        {
            try
            {
                var filmList  = new List <FilmVM>();
                var orderList = new List <Order>();

                using (var db = new VideoDB())
                {
                    var kunde = db.Users.FirstOrDefault(k => k.Email == email);
                    orderList = db.Orders.Where(o => o.User.Id == kunde.Id).ToList();

                    foreach (var ordre in orderList)
                    {
                        foreach (var ordrelinje in ordre.OrderLines)
                        {
                            filmList.Add(ConvertFilm(ordrelinje.Film));
                        }
                    }
                }
                return(filmList);
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.FilmDAL.GetUserFilms(string email)", email, e);
                return(new List <FilmVM>());
            }
        }
 /// <summary>
 /// Gets all changelog items in the database.
 /// </summary>
 /// <returns>List of changelog items.</returns>
 public List <ChangeLogVM> AllChanges()
 {
     try
     {
         using (var db = new VideoDB())
         {
             List <ChangeLogVM> changes = new List <ChangeLogVM>();
             foreach (var change in db.ChangeLogs)
             {
                 changes.Add(new ChangeLogVM()
                 {
                     EntityName      = change.EntityName,
                     PropertyName    = change.PropertyName,
                     PrimaryKeyValue = change.PrimaryKeyValue,
                     OldValue        = change.OldValue,
                     NewValue        = change.NewValue,
                     DateChanged     = TimeZoneInfo.ConvertTimeFromUtc(change.DateChanged, tZone).ToString("dd.MM.yyyy HH:mm:ss")
                 });
             }
             return(changes);
         }
     }
     catch (Exception e)
     {
         new ErrorLogRepository().CreateError("Oblig1.DAL.Repositories.OrdreRepository.AllChanges", null, e);
         return(new List <ChangeLogVM>());
     }
 }
예제 #11
0
 /// <summary>
 /// Get UserVM based on userID parameter.
 /// </summary>
 /// <param name="userID"></param>
 /// <returns></returns>
 public UserVM GetUserVM(int userID)
 {
     using (var db = new VideoDB())
     {
         User c = db.Users.FirstOrDefault(K => K.Id == userID);
         return(ConvertUser(c));
     }
 }
예제 #12
0
 /// <summary>
 /// Search for genre VM based on the genre's id.
 /// </summary>
 /// <param name="id">Genre id to search for.</param>
 /// <returns>GenreVM</returns>
 public GenreVM GetGenre(int id)
 {
     using (var db = new VideoDB())
     {
         Genre genre = db.Genres.FirstOrDefault(g => g.Id == id);
         return(ConvertGenre(genre));
     }
 }
예제 #13
0
 /// <summary>
 /// Search for genre VM based on the genre name.
 /// </summary>
 /// <param name="name">Genre name to search for.</param>
 /// <returns>GenreVM</returns>
 public GenreVM GetGenre(string name)
 {
     using (var db = new VideoDB())
     {
         Genre genre = db.Genres.FirstOrDefault(g => g.Name == name);
         return(ConvertGenre(genre));
     }
 }
예제 #14
0
        /// <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!");
            }
        }
예제 #15
0
        /// <summary>
        /// Edits the row in the database table that corresponds to the provided input film
        /// with information encapsulated in the wrapper class object.
        /// </summary>
        /// <param name="editAddFilm">The wrapper class for creating/editing films</param>
        /// <param name="priceClassId">PK for the price class that is selected</param>
        /// <param name="genreIds">PKs for the selected genres</param>
        /// <returns>An empty string if successful, an error message if not</returns>
        public string EditFilm(AddFilmVM editAddFilm, int priceClassId, int[] genreIds)
        {
            var editFilm = new FilmVM()
            {
                Id          = editAddFilm.FilmId,
                Title       = editAddFilm.Title,
                Description = editAddFilm.Description,
                ImgURL      = editAddFilm.ImgURL
            };

            try
            {
                using (var db = new VideoDB())
                {
                    Film film = db.Films.Find(editFilm.Id);
                    film.Title       = editFilm.Title;
                    film.Description = editFilm.Description;
                    film.ImgURL      = editFilm.ImgURL;

                    PriceClass findPriceClass = db.PriceClasses.Find(priceClassId);
                    film.PriceClasses = findPriceClass;
                    film.PriceClassId = priceClassId;

                    //Bug i EF? Må iterere gjennom listen for å nullstille
                    foreach (Genre g in film.Genres)
                    {
                    }

                    if (genreIds != null)
                    {
                        film.Genres.RemoveRange(0, film.Genres.Count);
                        foreach (int genreId in genreIds)
                        {
                            film.Genres.Add(db.Genres.Find(genreId));
                        }
                    }

                    string gIDs = "";

                    if (genreIds.Length > 0)
                    {
                        gIDs = string.Join(" ", genreIds);
                    }
                    film.GenreIds = gIDs;

                    db.SaveChanges();
                    return("");
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.FilmDAL.EditFilm(FilmVM newFilm)", editFilm.ToString(), e);
                return("Kunne ikke lagre filmen i databasen, vennligst kontakt kundeservice!");
            }
        }
예제 #16
0
        /// <summary>
        /// Returnes a list of genre vms, based on a sorting type, and where the
        /// number of films associated with it is at a minimum of filmCount.
        /// </summary>
        /// <param name="filmCount">Minimum number of films associated with the genre.</param>
        /// <param name="sort">Sorting algorithm to base the list on.</param>
        /// <returns>A list of genre vms</returns>
        /// <seealso cref="Props"/>
        public List <GenreVM> GetGenres(int filmCount, Sort sort = Sort.alfa)
        {
            using (var db = new VideoDB())
            {
                List <Genre> GenresOverCount = db.Genres.Where(g => g.Films.Count() >= filmCount).ToList();
                GenresOverCount = SortGenre(GenresOverCount, sort);
                List <GenreVM> GenreList = ConvertGenres(GenresOverCount);

                return(GenreList);
            }
        }
예제 #17
0
        /// <summary>
        /// Returns a list of genres, based on a sorting type.
        /// </summary>
        /// <param name="sort">Sorting type to base the sort on.</param>
        /// <returns>List of genre vms.</returns>
        /// <seealso cref="Props"/>
        public List <GenreVM> GetGenres(Sort sort = Sort.alfa)
        {
            using (var db = new VideoDB())
            {
                List <Genre> AllGenres = db.Genres.OrderBy(g => g.Name).ToList();
                AllGenres = SortGenre(AllGenres, sort);
                List <GenreVM> Genres = ConvertGenres(AllGenres);

                return(Genres);
            }
        }
예제 #18
0
        /// <summary>
        /// Gets all users from the database.
        /// </summary>
        /// <returns>A list of UserVMs.</returns>
        public List <UserVM> AllUsers()
        {
            using (var db = new VideoDB())
            {
                List <User> User = db.Users.ToList();

                List <UserVM> UserVMLIst = ConvertUsers(User);

                return(UserVMLIst);
            }
        }
예제 #19
0
 /// <summary>
 /// Checks if a email exists in the database.
 /// </summary>
 /// <param name="email">Email to check</param>
 /// <returns>False if the email doesnt exist, else true.
 /// </returns>
 public bool FindEmail(String email)
 {
     using (var db = new VideoDB())
     {
         var User = db.Users.FirstOrDefault(k => k.Email == email);
         if (User != null)
         {
             return(true);
         }
     }
     return(false);
 }
예제 #20
0
        /// <summary>
        /// Creates a new film from the provided data and initializes a new row in the film
        /// database table with the required information returned from the view.
        /// </summary>
        /// <param name="newAddFilm">The wrapper class for creating/editing films</param>
        /// <param name="priceClassId">PK for the selected price class</param>
        /// <param name="genreIds">PKs for the selected genres</param>
        /// <returns>An empty string if the operations were successful, otherwise an error message</returns>
        public string CreateFilm(AddFilmVM newAddFilm, int priceClassId, int[] genreIds)
        {
            var newFilm = new FilmVM()
            {
                Title       = newAddFilm.Title,
                Description = newAddFilm.Description,
                ImgURL      = newAddFilm.ImgURL
            };

            try
            {
                using (var db = new VideoDB())
                {
                    var newDBFilm = new Film
                    {
                        Title       = newFilm.Title,
                        Description = newFilm.Description,
                        ImgURL      = newFilm.ImgURL
                    };

                    PriceClass findPriceClass = db.PriceClasses.Find(priceClassId);
                    newDBFilm.PriceClasses = findPriceClass;

                    newDBFilm.PriceClassId = priceClassId;

                    List <Genre> genreList   = new List <Genre>();
                    List <int>   genreIdList = new List <int>();
                    foreach (int genreId in genreIds)
                    {
                        genreList.Add(db.Genres.Find(genreId));
                        genreIdList.Add(genreId);
                    }

                    newDBFilm.Genres = genreList;
                    string gIDs = "";

                    if (genreIds.Length > 0)
                    {
                        gIDs = string.Join(" ", genreIds);
                    }

                    newDBFilm.GenreIds = gIDs;
                    db.Films.Add(newDBFilm);
                    db.SaveChanges();
                    return("");
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.CustomerDAL.CreateFilm(FilmVM newFilm)", newFilm.ToString(), e);
                return("Kunne ikke lagre filmen i databasen, vennligst kontakt kundeservice!");
            }
        }
예제 #21
0
        public VideoView(int id)
        {
            VideoDB db = new VideoDB();

            this.video = new Video();
            if (id != 0)
            {
                this.video = db.Buscar(id);
            }
            this.tags       = db.Tags(id);
            this.trailers   = db.Trailers();
            this.categorias = db.Categorias(id);
            this.autores    = db.Autores(id);
        }
        /// <summary>
        /// Creates a list of ShoppingCartVMs, that represents the content in the shopping cart.
        /// </summary>
        /// <param name="selectedFilmIdList">A list of film Ids, that represents the shopping cart stored in session.</param>
        /// <returns>A list of shoppingCartVMs.</returns>
        public List <ShoppingCartVM> CreateShoppingCart(List <int> selectedFilmIdList)
        {
            try
            {
                if (selectedFilmIdList != null)
                {
                    var cartTableVM = new List <ShoppingCartVM>();
                    using (var db = new VideoDB())
                    {
                        var         sortedIdList = selectedFilmIdList.OrderBy(P => P).ToList();
                        List <Film> films        = db.Films.ToList();
                        List <Film> filmList     = new List <Film>();
                        int         index        = 0;
                        foreach (var film in films)
                        {
                            if (film.Id == sortedIdList[index])
                            {
                                filmList.Add(film);
                                index++;
                                if (index == sortedIdList.Count)
                                {
                                    break;
                                }
                            }
                        }

                        foreach (var film in filmList)
                        {
                            cartTableVM.Add(new ShoppingCartVM()
                            {
                                Id     = film.Id,
                                Title  = film.Title,
                                ImgURL = film.ImgURL,
                                Price  = film.PriceClasses.Price
                            });
                        }
                    }
                    return(cartTableVM);
                }

                return(new List <ShoppingCartVM>());
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.OrderRepository.CreateShoppingCart(List<int> selectedFilmIdList)", selectedFilmIdList.ToString(), e);

                //Returns new empty cart.
                return(new List <ShoppingCartVM>());
            }
        }
 /// <summary>
 /// Get number of changes.
 /// </summary>
 /// <returns></returns>
 public int GetChangeCount()
 {
     try
     {
         using (var db = new VideoDB())
         {
             return(db.ChangeLogs.Count());
         }
     }
     catch (Exception e)
     {
         new ErrorLogRepository().CreateError("Dal.UserRepository.GetChangeCount()", null, e);
         return(0);
     }
 }
        /// <summary>
        /// Gets all of customers orders.
        /// </summary>
        /// <param name="email">To search for in the database.</param>
        /// <returns>A list of orderVMs.</returns>
        public List <OrderVM> AllOfUserOrders(string email)
        {
            try
            {
                var  orderVMlist = new List <OrderVM>();
                User customer;
                using (var db = new VideoDB())
                {
                    customer = db.Users.FirstOrDefault(k => k.Email == email);
                    List <Order> orders = db.Orders.Where(o => o.User.Id == customer.Id).ToList();
                    foreach (var ordre in orders)
                    {
                        var OrderVM = new OrderVM()
                        {
                            OrderNr = ordre.OrderNr,

                            //Setting the time to "Romance Standard Time after retrieval from database.
                            //Our server is running with UTC time zone.
                            Date = TimeZoneInfo.ConvertTimeFromUtc(ordre.Date, tZone).ToString("dd.MM.yyyy HH:mm:ss"),

                            OrderLines = ordre.OrderLines.Select(o => new OrdreLinjeVM()
                            {
                                Title = o.Film.Title,
                                Price = o.Price
                            }).ToList()
                        };

                        int totalPrice = 0;
                        foreach (var li in OrderVM.OrderLines)
                        {
                            totalPrice += li.Price;
                        }
                        ;
                        OrderVM.TotalPrice = totalPrice;

                        orderVMlist.Add(OrderVM);
                    }
                }
                return(orderVMlist);
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.OrderRepository.AllOfCustomersOrders(string email))", email, e);
                return(new List <OrderVM>());
            }
        }
예제 #25
0
        /// <summary>
        /// Check if user is admin
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsAdmin(int id)
        {
            try
            {
                using (var db = new VideoDB())
                {
                    var user = db.Users.Find(id);

                    return(user.Admin);
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.UserDAL.IsAdmin(int id))", id.ToString(), e);
                return(false);
            }
        }
예제 #26
0
        /// <summary>
        /// Gets all films in the database and returns them sorted if specified, or in the order
        /// they have in the database (no sorting/by Id) if not.
        /// </summary>
        /// <param name="sort"></param>
        /// <returns></returns>
        public List <FilmVM> GetFilms(Sort sort = Sort.none)
        {
            try
            {
                using (var db = new VideoDB())
                {
                    List <Film>   AllFilms   = SortFilms(db.Films.ToList(), sort);
                    List <FilmVM> AllFilmVMs = ConvertFilms(AllFilms);

                    return(AllFilmVMs);
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.FilmDAL.GetFilms(Sorter sort = Sorter.none)", "null", e);
                return(null);
            }
        }
예제 #27
0
 /// <summary>
 /// Sets user.active to true, false.
 /// </summary>
 /// <param name="id"></param>
 /// <returns></returns>
 public bool ToggleActivate(int id)
 {
     try
     {
         using (var db = new VideoDB())
         {
             var user = db.Users.FirstOrDefault(c => c.Id == id);
             user.Active = !user.Active;
             db.SaveChanges();
             return(true);
         }
     }
     catch (Exception e)
     {
         new ErrorLogRepository().CreateError("Oblig1.DAL.UserDAL.ToggleActivate(int  id)", id.ToString(), e);
         return(false);
     }
 }
예제 #28
0
        /// <summary>
        /// Gets all films in a genre and sorts it after the chosen sorting.
        /// </summary>
        /// <param name="genreName">The selected genre</param>
        /// <param name="sort">The sorting of the films</param>
        /// <seealso cref="GetFilms(Sorter)"/>
        /// <returns>A list of the appropriate films</returns>
        public List <FilmVM> GetFilms(string genreName, Sort sort = Sort.none)
        {
            try
            {
                using (var db = new VideoDB())
                {
                    Genre         Genre         = db.Genres.FirstOrDefault(g => g.Name == genreName);
                    List <Film>   GenresFilms   = SortFilms(Genre.Films.ToList(), sort);
                    List <FilmVM> GenreFilmList = ConvertFilms(GenresFilms);

                    return(GenreFilmList);
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.FilmDAL.GetFilms(string genreName, Sort sort = Sort.none)", "List<FilmVM>", e);
                return(new List <FilmVM>());
            }
        }
예제 #29
0
        /// <summary>
        /// Gets a new instance of the wrapper class for creating/editing
        /// films. Returns null if errors occur.
        /// </summary>
        /// <returns>The wrapper class for creating/editing films</returns>
        public AddFilmVM GetAddFilmVM()
        {
            AddFilmVM newAddFilmVM = new AddFilmVM();

            try
            {
                using (var db = new VideoDB())
                {
                    List <SelectListItem> GenreSelectList = db.Genres.AsNoTracking()
                                                            .OrderBy(n => n.Id)
                                                            .Select(n => new SelectListItem
                    {
                        Value = n.Id.ToString(),
                        Text  = n.Name.ToString()
                    }).ToList();

                    List <SelectListItem> PriceClassSelectList = db.PriceClasses.AsNoTracking()
                                                                 .OrderBy(n => n.Id)
                                                                 .Select(n => new SelectListItem
                    {
                        Value = n.Id.ToString(),
                        Text  = "Prisklasse " + n.Id + " (" + n.Price + " kr)"
                    }).ToList();
                    var listTop = new SelectListItem()
                    {
                        Value = null,
                        Text  = "--- Velg Prisklasse ---"
                    };
                    PriceClassSelectList.Insert(0, listTop);

                    newAddFilmVM.PriceSelectList = PriceClassSelectList;
                    newAddFilmVM.GenreSelectList = GenreSelectList;

                    return(newAddFilmVM);
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.FilmDAL.GetAddFilmVM()", "Kunne ikke opprette AddFilmVM", e);
                return(null);
            }
        }
예제 #30
0
        /// <summary>
        /// Saving price changes to the database.
        /// </summary>
        /// <param name="id">Price class id</param>
        /// <param name="newPrice">New price value</param>
        /// <returns>True if successful, else false.</returns>
        public bool ChangePrice(int id, int newPrice)
        {
            try
            {
                using (var db = new VideoDB())
                {
                    PriceClass Price = db.PriceClasses.Find(id);
                    Price.Price = newPrice;
                    db.SaveChanges();

                    return(true);
                }
            }
            catch (Exception e)
            {
                new ErrorLogRepository().CreateError("Oblig1.DAL.PriceRepository.ChangePrice(int id, int newPrice)", id.ToString() + "/r/n" + newPrice.ToString(), e);

                return(false);
            }
        }