Beispiel #1
0
        private async void dgvMovies_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                var      movieId  = dgvMovies.Rows[e.RowIndex].Cells["MovieId"].Value;
                var      action   = dgvMovies.Columns[e.ColumnIndex].Name;
                MenuForm menuForm = (MenuForm)this.MdiParent;
                var      movie    = await _apiService.GetById <Model.Movie>(movieId);

                CustomMessageBox messageBox = new CustomMessageBox();

                if (action == "Edit" || action == "Title")
                {
                    AddEditMovieForm form = new AddEditMovieForm(menuForm, int.Parse(movieId.ToString()))
                    {
                        MdiParent = menuForm,
                        Dock      = DockStyle.Fill
                    };
                    form.Show();
                }
                else if (action == "Delete")
                {
                    DialogResult dialogResult = MessageBox.Show($"Are you sure you want to permanently delete '{movie.Title}'?", "Delete movie?", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        await _apiService.Delete <Model.Movie>(movieId);

                        foreach (Form frm in menuForm.MdiChildren)
                        {
                            frm.Close();
                        }
                        MoviesForm form = new MoviesForm {
                            MdiParent = menuForm,
                            Dock      = DockStyle.Fill
                        };
                        form.Show();
                        messageBox.Show("Movie deleted successfully", "success");
                    }
                }
                else
                {
                    return;
                }
            }
        }
        private async void SaveBtn_Click(object sender, EventArgs e)
        {
            var messageBox = new CustomMessageBox();

            if (string.IsNullOrWhiteSpace(MovieTitle.Text) || MovieTitle.Text.Length < 4)
            {
                messageBox.Show("The title field requires 4 letters!", "error");
                return;
            }

            if (string.IsNullOrWhiteSpace(Description.Text) || Description.Text.Length < 5)
            {
                messageBox.Show("The description field requires 5 letters!", "error");
                return;
            }

            if (string.IsNullOrWhiteSpace(Cast.Text) || Cast.Text.Length < 15)
            {
                messageBox.Show("The cast field requires 15 letters!", "error");
                return;
            }
            if (string.IsNullOrWhiteSpace(ImageLink.Text) || !ImageLink.Text.Contains(".jpg"))
            {
                messageBox.Show("Enter a valid '.jpg' image link!", "error");
                return;
            }
            if (string.IsNullOrWhiteSpace(TrailerLink.Text))
            {
                messageBox.Show("Enter a trailer id", "error");
                return;
            }

            if (string.IsNullOrWhiteSpace(Rating.Text) || !decimal.TryParse(Rating.Text, out decimal n))
            {
                messageBox.Show("Enter a valid rating (0-10)!", "error");
                return;
            }
            decimal rating = decimal.Parse(Rating.Text);

            if (rating < 0 || rating > 10)
            {
                messageBox.Show("Enter a valid rating (0-10)!", "error");
                return;
            }

            if (string.IsNullOrWhiteSpace(Price.Text) || !decimal.TryParse(Price.Text, out decimal m))
            {
                messageBox.Show("Enter a valid price (1-200)!", "error");
                return;
            }
            decimal price = decimal.Parse(Price.Text);

            if (price < 1 || price > 200)
            {
                messageBox.Show("Enter a valid price (1-200)!", "error");
                return;
            }
            if (Year.Value < Year.Minimum || Year.Value > Year.Maximum)
            {
                messageBox.Show("Enter a valid year (1800-2025)!", "error");
                return;
            }
            var hours = Duration.Value.Hour;

            if (hours < 0 || hours > 4)
            {
                messageBox.Show("Enter a valid duration (up to 5 hours)!", "error");
                return;
            }

            if (string.IsNullOrWhiteSpace(Genre.Text) || Genre.Text.Length < 4)
            {
                messageBox.Show("The genre field requires 4 letters!", "error");
                return;
            }

            Model.Requests.InsertMovieRequest request = new Model.Requests.InsertMovieRequest()
            {
                Title       = MovieTitle.Text,
                Year        = (int)Year.Value,
                Duration    = Duration.Value.ToString("HH:mm"),
                Rating      = decimal.Parse(Rating.Text),
                Description = Description.Text,
                Cast        = Cast.Text,
                ImageLink   = ImageLink.Text,
                Standalone  = Standalone.Checked,
                Price       = decimal.Parse(Price.Text),
                TrailerLink = $"https://www.youtube.com/embed/{TrailerLink.Text}",
                Genre       = Genre.Text
            };

            if (_movieId.HasValue)
            {
                await _apiService.Update <Model.Movie>(_movieId, request);
            }
            else
            {
                await _apiService.Insert <Model.Movie>(request);
            }

            this.Close();
            foreach (Form frm in _menuForm.MdiChildren)
            {
                frm.Close();
            }
            MoviesForm form = new MoviesForm {
                MdiParent = _menuForm,
                Dock      = DockStyle.Fill
            };

            form.Show();
            if (_movieId.HasValue)
            {
                messageBox.Show("Movie updated successfully!", "success");
            }
            else
            {
                messageBox.Show("Movie added successfully!", "success");
            }
        }