예제 #1
0
        private void btnToevoegen_Click(object sender, RoutedEventArgs e)
        {
            bool          filmOwned           = false;
            GebruikerFilm gebruikerFilmToEdit = new GebruikerFilm();

            #region Controle dat nodige selecties gemaakt zijn
            if (cmbFilmNaam.SelectedIndex == -1 || cmbAankoopType.SelectedIndex == -1)
            {
                lblFout.Text = "Zorg dat een film en aankooptype geselecteerd zijn.";
                ShowLabel();

                return;
            }
            #endregion

            Film filmToAdd = cmbFilmNaam.SelectedItem as Film;

            // Controleren of film al gekocht/gehuurd is
            foreach (var gebruikerFilm in gebruikerFilms)
            {
                if (gebruikerFilm.filmId == filmToAdd.id)
                {
                    filmOwned = true;
                    #region instellen gebruikerfilm-object
                    gebruikerFilmToEdit.id            = gebruikerFilm.id;
                    gebruikerFilmToEdit.filmId        = gebruikerFilm.filmId;
                    gebruikerFilmToEdit.gebruikerId   = gebruikerFilm.gebruikerId;
                    gebruikerFilmToEdit.aankoopTypeId = gebruikerFilm.aankoopTypeId;
                    #endregion
                }
            }

            if (filmOwned)
            {
                #region Film is reeds gekocht of gehuurd
                // Als film al gekocht is, dit tonen aan gebruiker
                if (gebruikerFilmToEdit.aankoopTypeId == DatabaseOperations.AankoopTypeOpBeschrijving("Gekocht").id)
                {
                    lblFout.Text = "Je hebt deze film reeds aangekocht.";
                    ShowLabel();
                }
                // Als film al gehuurd is en huren is aangeduid, dit tonen aan gebruiker
                else if (gebruikerFilmToEdit.aankoopTypeId == DatabaseOperations.AankoopTypeOpBeschrijving("Gehuurd").id&& cmbAankoopType.SelectedIndex == 1)
                {
                    lblFout.Text = "Je bent deze film reeds aan het huren.";
                    ShowLabel();
                }
                // Als de film reeds gehuurd is en gebruiker wil kopen, omzetten naar gekocht
                else
                {
                    gebruikerFilmToEdit.aankoopTypeId = DatabaseOperations.AankoopTypeOpBeschrijving("Gekocht").id;

                    int ok = DatabaseOperations.AanpassenGebruikerFilm(gebruikerFilmToEdit);

                    if (ok > 0)
                    {
                        ResetDatagrid();
                    }
                    else
                    {
                        lblFout.Text = "Er ging iets mis bij het aanpassen van de film. Probeer opnieuw.";
                        ShowLabel();
                    }
                }
                #endregion
            }
            else
            {
                // Film nog niet in bezit
                #region Aanmaken GebruikerFilm-object
                GebruikerFilm nieuweFilm = new GebruikerFilm();
                nieuweFilm.filmId        = filmToAdd.id;
                nieuweFilm.gebruikerId   = Gebruiker.id;
                nieuweFilm.aankoopTypeId = DatabaseOperations.AankoopTypeOpBeschrijving(cmbAankoopType.Text).id;
                #endregion

                #region Toevoegen nieuwe GebruikerFilm
                int ok = DatabaseOperations.ToevoegenGebruikerFilm(nieuweFilm);

                if (ok > 0)
                {
                    ResetDatagrid();
                }
                else
                {
                    lblFout.Text = "Er ging iets mis bij het toevoegen van de film aan de gebruiker. Probeer opnieuw.";
                    ShowLabel();
                }
                #endregion
            }
        }