diff --git a/ExamNETIntermediate/Form1.Designer.cs b/ExamNETIntermediate/Form1.Designer.cs index ece918a..f81f5f5 100644 --- a/ExamNETIntermediate/Form1.Designer.cs +++ b/ExamNETIntermediate/Form1.Designer.cs @@ -28,12 +28,341 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; + listBoxSongList = new ListBox(); + buttonRefresh = new Button(); + numericUpDownMinutes = new NumericUpDown(); + tableLayoutPanelForm = new TableLayoutPanel(); + textBoxArtist = new TextBox(); + labelTitle = new Label(); + labelArtists = new Label(); + labelGenre = new Label(); + numericUpDownSeconds = new NumericUpDown(); + labelSeconds = new Label(); + labelReleaseDate = new Label(); + dateTimePickerReleaseDate = new DateTimePicker(); + checkBoxIsAvailable = new CheckBox(); + textBoxTitle = new TextBox(); + comboBoxGenre = new ComboBox(); + labelMinutes = new Label(); + labelNotification = new Label(); + tableLayoutPanel1 = new TableLayoutPanel(); + buttonAdd = new Button(); + buttonDelete = new Button(); + buttonEdit = new Button(); + textBoxSearch = new TextBox(); + labelSearch = new Label(); + labelAppTitle = new Label(); + listBoxSongList2 = new ListBox(); + ((System.ComponentModel.ISupportInitialize)numericUpDownMinutes).BeginInit(); + tableLayoutPanelForm.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)numericUpDownSeconds).BeginInit(); + tableLayoutPanel1.SuspendLayout(); + SuspendLayout(); + // + // listBoxSongList + // + listBoxSongList.FormattingEnabled = true; + listBoxSongList.Location = new Point(12, 44); + listBoxSongList.Name = "listBoxSongList"; + listBoxSongList.Size = new Size(263, 364); + listBoxSongList.TabIndex = 0; + listBoxSongList.SelectedIndexChanged += ListBoxSongList_SelectedIndexChanged; + // + // buttonRefresh + // + buttonRefresh.Location = new Point(302, 328); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(94, 28); + buttonRefresh.TabIndex = 1; + buttonRefresh.Text = "Refresh"; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += ButtonRefresh_Click; + // + // numericUpDownMinutes + // + numericUpDownMinutes.Location = new Point(3, 146); + numericUpDownMinutes.Name = "numericUpDownMinutes"; + numericUpDownMinutes.Size = new Size(64, 27); + numericUpDownMinutes.TabIndex = 2; + // + // tableLayoutPanelForm + // + tableLayoutPanelForm.Anchor = AnchorStyles.Left | AnchorStyles.Right; + tableLayoutPanelForm.ColumnCount = 2; + tableLayoutPanelForm.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 26.5402851F)); + tableLayoutPanelForm.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 73.45972F)); + tableLayoutPanelForm.Controls.Add(textBoxArtist, 1, 1); + tableLayoutPanelForm.Controls.Add(labelTitle, 0, 0); + tableLayoutPanelForm.Controls.Add(labelArtists, 0, 1); + tableLayoutPanelForm.Controls.Add(labelGenre, 0, 2); + tableLayoutPanelForm.Controls.Add(numericUpDownMinutes, 0, 4); + tableLayoutPanelForm.Controls.Add(numericUpDownSeconds, 1, 4); + tableLayoutPanelForm.Controls.Add(labelSeconds, 1, 3); + tableLayoutPanelForm.Controls.Add(labelReleaseDate, 0, 5); + tableLayoutPanelForm.Controls.Add(dateTimePickerReleaseDate, 1, 5); + tableLayoutPanelForm.Controls.Add(checkBoxIsAvailable, 1, 6); + tableLayoutPanelForm.Controls.Add(textBoxTitle, 1, 0); + tableLayoutPanelForm.Controls.Add(comboBoxGenre, 1, 2); + tableLayoutPanelForm.Controls.Add(labelMinutes, 0, 3); + tableLayoutPanelForm.Location = new Point(299, 44); + tableLayoutPanelForm.Name = "tableLayoutPanelForm"; + tableLayoutPanelForm.RowCount = 7; + tableLayoutPanelForm.RowStyles.Add(new RowStyle(SizeType.Percent, 50F)); + tableLayoutPanelForm.RowStyles.Add(new RowStyle(SizeType.Absolute, 32F)); + tableLayoutPanelForm.RowStyles.Add(new RowStyle(SizeType.Absolute, 34F)); + tableLayoutPanelForm.RowStyles.Add(new RowStyle(SizeType.Absolute, 24F)); + tableLayoutPanelForm.RowStyles.Add(new RowStyle(SizeType.Percent, 50F)); + tableLayoutPanelForm.RowStyles.Add(new RowStyle(SizeType.Absolute, 32F)); + tableLayoutPanelForm.RowStyles.Add(new RowStyle(SizeType.Absolute, 51F)); + tableLayoutPanelForm.Size = new Size(474, 279); + tableLayoutPanelForm.TabIndex = 3; + // + // textBoxArtist + // + textBoxArtist.Location = new Point(128, 56); + textBoxArtist.Name = "textBoxArtist"; + textBoxArtist.Size = new Size(250, 27); + textBoxArtist.TabIndex = 14; + // + // labelTitle + // + labelTitle.AutoSize = true; + labelTitle.Location = new Point(3, 0); + labelTitle.Name = "labelTitle"; + labelTitle.Size = new Size(38, 20); + labelTitle.TabIndex = 6; + labelTitle.Text = "Title"; + // + // labelArtists + // + labelArtists.AutoSize = true; + labelArtists.Location = new Point(3, 53); + labelArtists.Name = "labelArtists"; + labelArtists.Size = new Size(44, 20); + labelArtists.TabIndex = 7; + labelArtists.Text = "Artist"; + // + // labelGenre + // + labelGenre.AutoSize = true; + labelGenre.Location = new Point(3, 85); + labelGenre.Name = "labelGenre"; + labelGenre.Size = new Size(48, 20); + labelGenre.TabIndex = 8; + labelGenre.Text = "Genre"; + // + // numericUpDownSeconds + // + numericUpDownSeconds.Location = new Point(128, 146); + numericUpDownSeconds.Name = "numericUpDownSeconds"; + numericUpDownSeconds.Size = new Size(64, 27); + numericUpDownSeconds.TabIndex = 3; + // + // labelSeconds + // + labelSeconds.Anchor = AnchorStyles.Left | AnchorStyles.Right; + labelSeconds.AutoSize = true; + labelSeconds.Location = new Point(128, 121); + labelSeconds.Name = "labelSeconds"; + labelSeconds.Size = new Size(343, 20); + labelSeconds.TabIndex = 5; + labelSeconds.Text = "Seconds"; + // + // labelReleaseDate + // + labelReleaseDate.Anchor = AnchorStyles.Left | AnchorStyles.Right; + labelReleaseDate.AutoSize = true; + labelReleaseDate.Location = new Point(3, 202); + labelReleaseDate.Name = "labelReleaseDate"; + labelReleaseDate.Size = new Size(119, 20); + labelReleaseDate.TabIndex = 9; + labelReleaseDate.Text = "ReleaseDate"; + // + // dateTimePickerReleaseDate + // + dateTimePickerReleaseDate.Location = new Point(128, 199); + dateTimePickerReleaseDate.Name = "dateTimePickerReleaseDate"; + dateTimePickerReleaseDate.Size = new Size(250, 27); + dateTimePickerReleaseDate.TabIndex = 10; + // + // checkBoxIsAvailable + // + checkBoxIsAvailable.AutoSize = true; + checkBoxIsAvailable.Location = new Point(128, 231); + checkBoxIsAvailable.Name = "checkBoxIsAvailable"; + checkBoxIsAvailable.Size = new Size(103, 24); + checkBoxIsAvailable.TabIndex = 12; + checkBoxIsAvailable.Text = "IsAvailable"; + checkBoxIsAvailable.UseVisualStyleBackColor = true; + // + // textBoxTitle + // + textBoxTitle.Location = new Point(128, 3); + textBoxTitle.Name = "textBoxTitle"; + textBoxTitle.Size = new Size(250, 27); + textBoxTitle.TabIndex = 13; + // + // comboBoxGenre + // + comboBoxGenre.FormattingEnabled = true; + comboBoxGenre.Location = new Point(128, 88); + comboBoxGenre.Name = "comboBoxGenre"; + comboBoxGenre.Size = new Size(250, 28); + comboBoxGenre.TabIndex = 15; + // + // labelMinutes + // + labelMinutes.Anchor = AnchorStyles.Left | AnchorStyles.Right; + labelMinutes.AutoSize = true; + labelMinutes.Location = new Point(3, 121); + labelMinutes.Name = "labelMinutes"; + labelMinutes.Size = new Size(119, 20); + labelMinutes.TabIndex = 4; + labelMinutes.Text = "Minutes"; + // + // labelNotification + // + labelNotification.Anchor = AnchorStyles.Left | AnchorStyles.Right; + labelNotification.AutoSize = true; + labelNotification.Location = new Point(413, 332); + labelNotification.Name = "labelNotification"; + labelNotification.Size = new Size(88, 20); + labelNotification.TabIndex = 5; + labelNotification.Text = "Notification"; + // + // tableLayoutPanel1 + // + tableLayoutPanel1.Anchor = AnchorStyles.Left | AnchorStyles.Right; + tableLayoutPanel1.ColumnCount = 3; + tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F)); + tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F)); + tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 199F)); + tableLayoutPanel1.Controls.Add(buttonAdd, 0, 0); + tableLayoutPanel1.Controls.Add(buttonDelete, 2, 0); + tableLayoutPanel1.Controls.Add(buttonEdit, 1, 0); + tableLayoutPanel1.Location = new Point(299, 362); + tableLayoutPanel1.Name = "tableLayoutPanel1"; + tableLayoutPanel1.RowCount = 1; + tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 50F)); + tableLayoutPanel1.Size = new Size(433, 34); + tableLayoutPanel1.TabIndex = 6; + // + // buttonAdd + // + buttonAdd.Location = new Point(3, 3); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(94, 27); + buttonAdd.TabIndex = 4; + buttonAdd.Text = "Add"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += ButtonAdd_Click; + // + // buttonDelete + // + buttonDelete.Location = new Point(237, 3); + buttonDelete.Name = "buttonDelete"; + buttonDelete.Size = new Size(94, 27); + buttonDelete.TabIndex = 6; + buttonDelete.Text = "Delete"; + buttonDelete.UseVisualStyleBackColor = true; + buttonDelete.Click += ButtonDelete_Click; + // + // buttonEdit + // + buttonEdit.Location = new Point(120, 3); + buttonEdit.Name = "buttonEdit"; + buttonEdit.Size = new Size(87, 27); + buttonEdit.TabIndex = 5; + buttonEdit.Text = "Edit"; + buttonEdit.UseVisualStyleBackColor = true; + buttonEdit.Click += ButtonEdit_Click; + // + // textBoxSearch + // + textBoxSearch.Location = new Point(803, 73); + textBoxSearch.Name = "textBoxSearch"; + textBoxSearch.Size = new Size(161, 27); + textBoxSearch.TabIndex = 7; + textBoxSearch.TextChanged += TextBoxSearch_TextChanged; + // + // labelSearch + // + labelSearch.AutoSize = true; + labelSearch.Location = new Point(803, 50); + labelSearch.Name = "labelSearch"; + labelSearch.Size = new Size(207, 20); + labelSearch.TabIndex = 8; + labelSearch.Text = "Search by Title or Artist Name"; + // + // labelAppTitle + // + labelAppTitle.AutoSize = true; + labelAppTitle.Font = new Font("Segoe UI", 12F, FontStyle.Regular, GraphicsUnit.Point, 0); + labelAppTitle.Location = new Point(12, 9); + labelAppTitle.Name = "labelAppTitle"; + labelAppTitle.Size = new Size(273, 28); + labelAppTitle.TabIndex = 9; + labelAppTitle.Text = "Music Management Metadata"; + // + // listBoxSongList2 + // + listBoxSongList2.FormattingEnabled = true; + listBoxSongList2.Location = new Point(12, 44); + listBoxSongList2.Name = "listBoxSongList2"; + listBoxSongList2.Size = new Size(263, 364); + listBoxSongList2.TabIndex = 10; + // + // Form1 + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1101, 450); + Controls.Add(labelAppTitle); + Controls.Add(labelSearch); + Controls.Add(textBoxSearch); + Controls.Add(labelNotification); + Controls.Add(buttonRefresh); + Controls.Add(tableLayoutPanel1); + Controls.Add(tableLayoutPanelForm); + Controls.Add(listBoxSongList); + Controls.Add(listBoxSongList2); + Name = "Form1"; + Text = "Form1"; + ((System.ComponentModel.ISupportInitialize)numericUpDownMinutes).EndInit(); + tableLayoutPanelForm.ResumeLayout(false); + tableLayoutPanelForm.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)numericUpDownSeconds).EndInit(); + tableLayoutPanel1.ResumeLayout(false); + ResumeLayout(false); + PerformLayout(); } #endregion + + private ListBox listBoxSongList; + private Button buttonRefresh; + private NumericUpDown numericUpDownMinutes; + private TableLayoutPanel tableLayoutPanelForm; + private NumericUpDown numericUpDownSeconds; + private Label labelMinutes; + private Label labelSeconds; + private Label labelTitle; + private Label labelArtists; + private Label labelGenre; + private Label labelReleaseDate; + private DateTimePicker dateTimePickerReleaseDate; + private CheckBox checkBoxIsAvailable; + private TextBox textBoxArtist; + private TextBox textBoxTitle; + private Label labelNotification; + private ComboBox comboBoxGenre; + private TableLayoutPanel tableLayoutPanel1; + private Button buttonAdd; + private Button buttonEdit; + private Button buttonDelete; + private TextBox textBoxSearch; + private Label labelSearch; + private Label labelAppTitle; + private ListBox listBoxSongList2; } } diff --git a/ExamNETIntermediate/Form1.cs b/ExamNETIntermediate/Form1.cs index 3302c50..60a7537 100644 --- a/ExamNETIntermediate/Form1.cs +++ b/ExamNETIntermediate/Form1.cs @@ -1,10 +1,372 @@ +using Newtonsoft; +using Newtonsoft.Json; + namespace ExamNETIntermediate { + public partial class Form1 : Form { + + //Define an instance of Model to help storing data. + public List Songs { get; set; } = new List(); + public List Genres { get; set; } = new List(); + public HttpClient _httpClient { get; set; } = new HttpClient(); + //Define a field called DateMaximal for conditional maximum 7 days. + private DateTimeOffset DateMaximal { get; } = DateTimeOffset.Now.AddDays(7); + + //Constructor public Form1() { InitializeComponent(); + PopulateListBox(); + PopulateGenreComboBox(); + buttonEdit.Visible = false; + buttonDelete.Visible = false; + labelNotification.Visible = false; + } + //Methods + + /// + /// To Clear Inputs + /// + private void ClearBox() + { + textBoxTitle.Text = string.Empty; + textBoxArtist.Text = string.Empty; + comboBoxGenre.Text = string.Empty; + numericUpDownMinutes.Value = 0; + numericUpDownSeconds.Value = 0; + listBoxSongList.SelectedItem = null; + checkBoxIsAvailable.Checked = false; + buttonAdd.Visible = true; + buttonEdit.Visible = false; + buttonDelete.Visible = false; + + } + + /// + /// To Calling API storing data for ComboBox(genre) and ListBox(song) + /// + private async void PopulateGenreComboBox() + { + var response = await _httpClient.GetAsync("https://new-dev.accelist.com:10000/api/genre"); + response.EnsureSuccessStatusCode(); + var jsonContent = await response.Content.ReadAsStringAsync(); + var content = JsonConvert.DeserializeObject>(jsonContent); + + if (content != null) + { + Genres = content; + } + comboBoxGenre.Items.Clear(); + comboBoxGenre.Items.AddRange(Genres.ToArray()); + comboBoxGenre.DisplayMember = "GenreName"; + } + private async void PopulateListBox() + { + var response = await _httpClient.GetAsync("https://new-dev.accelist.com:10000/api/song"); + response.EnsureSuccessStatusCode(); + var jsonContent = await response.Content.ReadAsStringAsync(); + var content = JsonConvert.DeserializeObject>(jsonContent); + + //Storing content to songs + if (content != null) + { + Songs = content; + } + listBoxSongList.Items.Clear(); + listBoxSongList.Items.AddRange(Songs.ToArray()); + listBoxSongList.DisplayMember = "Title"; + } + + /// + /// For changing behavior when select index in Listbox + /// + private void ListBoxSongList_SelectedIndexChanged(object sender, EventArgs e) + { + //Requirement when song is selected there's only edit and delete button also refresh button will stay. + buttonAdd.Visible = false; + buttonEdit.Visible = true; + buttonDelete.Visible = true; + + + var selectedItem = listBoxSongList.SelectedItem as Song; + if (selectedItem == null) + { + return; + } + + //Changing input textbox as same as data song in the listbox + textBoxTitle.Text = selectedItem.Title; + textBoxArtist.Text = selectedItem.Artist; + var currentGenre = Genres.Where(Q => Q.GenreName == selectedItem.GenreName).FirstOrDefault(); + comboBoxGenre.SelectedItem = currentGenre; + numericUpDownMinutes.Value = selectedItem.Length / 60; + numericUpDownSeconds.Value = selectedItem.Length % 60; + dateTimePickerReleaseDate.Value = selectedItem.ReleaseDate.DateTime; + checkBoxIsAvailable.Checked = selectedItem.IsAvailable; + } + + /// + /// Refresh button method, updating data to server + /// + private void ButtonRefresh_Click(object sender, EventArgs e) + { + labelNotification.Visible = true; + PopulateListBox(); + ClearBox(); + labelNotification.Text = "Refreshing Song List"; + } + + /// + /// Add new song to list, by fill all input. using api + /// + private async void ButtonAdd_Click(object sender, EventArgs e) + { + //defining fields needed in api + labelNotification.Visible = true; + var title = textBoxTitle.Text; + var artist = textBoxArtist.Text; + var genre = comboBoxGenre.Text; + var length = numericUpDownMinutes.Value * 60 + numericUpDownSeconds.Value; + var releaseDate = dateTimePickerReleaseDate.Value; + + //Validation Input + if (string.IsNullOrEmpty(title)) + { + labelNotification.Text = "Title can't be empty"; + return; + } + else if (string.IsNullOrEmpty(artist)) + { + labelNotification.Text = "Artist can't be empty"; + return; + } + else if (string.IsNullOrEmpty(genre)) + { + labelNotification.Text = "Genre can't be empty"; + return; + } + else if (genre != "Pop" && genre != "Electronic" && genre != "Rock") + { + labelNotification.Text = "Only 3 Genre is available: Pop , Electronic & Rock"; + return; + } + else if (numericUpDownSeconds.Value < (decimal)0m && numericUpDownSeconds.Value > (decimal)59m) + { + labelNotification.Text = "Input Seconds between 0 - 59"; + return; + } + else if (numericUpDownMinutes.Value == (decimal)0m) + { + labelNotification.Text = "Please input minutes and seconds correctly"; + return; + } + else if (releaseDate > DateMaximal) + { + labelNotification.Text = "Release date will be 7 days after today at the latest"; + return; + } + else + { + labelNotification.Text = "Looks like you good to go"; + } + + var selectedGenre = comboBoxGenre.SelectedItem as Genre; + if (selectedGenre == null) + { + labelNotification.Text = "Genres can't be empty"; + return; + } + + //make an instance of model to store by api + var songModel = new SongInput + { + SongId = listBoxSongList.Items.Count + 1, + Title = title, + Artist = artist, + GenreId = selectedGenre.GenreId, + Length = (int)length, + ReleaseDate = releaseDate, + IsAvailable = checkBoxIsAvailable.Checked + }; + var jsonString = JsonConvert.SerializeObject(songModel); + var content = new StringContent(jsonString); + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); + + //Api calling + var response = await _httpClient.PostAsync("https://new-dev.accelist.com:10000/api/song", content); + response.EnsureSuccessStatusCode(); + + //Display result, success or failed + if (response.IsSuccessStatusCode) + { + labelNotification.Text = $"Successfully to insert Song : {title}"; + } + else + { + labelNotification.Text = "Failed to insert new song"; + } + + //updating listbox as new as added. + PopulateListBox(); + ClearBox(); + } + + /// + /// Edit button method, by fill all input. using api + /// + private async void ButtonEdit_Click(object sender, EventArgs e) + { + //defining fields as needed in api + labelNotification.Visible = true; + var selectedSong = listBoxSongList.SelectedItem as Song; + var title = textBoxTitle.Text; + var artist = textBoxArtist.Text; + var genre = comboBoxGenre.SelectedItem as Genre; + var length = numericUpDownMinutes.Value / 60 + numericUpDownSeconds.Value; + var releaseDate = dateTimePickerReleaseDate.Value; + var isAvailable = checkBoxIsAvailable.Checked; + + + //Validation input + if (string.IsNullOrEmpty(title)) + { + labelNotification.Text = "Title can't be empty"; + return; + } + else if (string.IsNullOrEmpty(artist)) + { + labelNotification.Text = "Artist can't be empty"; + return; + } + else if (numericUpDownSeconds.Value < (decimal)0m && numericUpDownSeconds.Value > (decimal)59m) + { + labelNotification.Text = "Input Seconds between 0 - 59"; + return; + } + else if (numericUpDownMinutes.Value == (decimal)0m && numericUpDownSeconds.Value == (decimal)0m) + { + labelNotification.Text = "Please input minutes and seconds correctly"; + return; + } + else if (releaseDate > DateMaximal) + { + labelNotification.Text = "Release date will be 7 days after today at the latest"; + return; + } + else if (genre == null) + { + labelNotification.Text = "Please choose a genre above"; + return; + } + else if (selectedSong == null) + { + //Keknya sih ga mungkin kesini yaa, soalnya tombol edit di hide kalo belom diselect lagunya + labelNotification.Text = "Please choose a song to edit"; + return; + } + else + { + labelNotification.Text = "Looks like you good to go"; + } + + //Make instance of a model to stored by api + var songEdit = new SongInput + { + SongId = selectedSong.SongId, + Title = title, + Artist = artist, + GenreId = genre.GenreId, + Length = (int)length, + ReleaseDate = releaseDate, + IsAvailable = isAvailable + }; + var jsonString = JsonConvert.SerializeObject(songEdit); + var content = new StringContent(jsonString); + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); + + //calling api + var response = await _httpClient.PutAsync("https://new-dev.accelist.com:10000/api/song", content); + response.EnsureSuccessStatusCode(); + + //display result either success or failed + if (response.IsSuccessStatusCode) + { + labelNotification.Text = "Success Updating Song"; + } + else + { + labelNotification.Text = "Failed to Update Song"; + } + + //updating listbox as new + PopulateListBox(); + ClearBox(); + } + + /// + /// delete method, by selected song in the list box. using api + /// + private async void ButtonDelete_Click(object sender, EventArgs e) + { + labelNotification.Visible = true; + + var selectedSong = listBoxSongList.SelectedItem as Song; + + //Validating if there's no selected song, but its impossible to reach here cause the button is hidden if no song is selected + if (selectedSong == null) + { + labelNotification.Text = "Please Choose a song to be deleted"; + return; + } + + //called api + var response = await _httpClient.DeleteAsync($"https://new-dev.accelist.com:10000/api/song/{selectedSong.SongId}"); + response.EnsureSuccessStatusCode(); + + //display result either success or failed + if (response.IsSuccessStatusCode) + { + labelNotification.Text = "Delete Song Successfully"; + } + else + { + labelNotification.Text = "Failed to Delete Song"; + } + + //updating list box + PopulateListBox(); + ClearBox(); + } + + /// + /// Search Method when text changed, works like filtering. + /// + private void TextBoxSearch_TextChanged(object sender, EventArgs e) + { + //ini ada 2 listbox , buat ngumpetin error. + //jadi yang fitur search bar itu datanya taro di list box 2.. soalnya kalo tetep taro di 1 udah ke binding gitu datanya.. jadi kalo mau refresh atau lakuin hal lain jadi error.. + var searchText = textBoxSearch.Text; + if(string.IsNullOrEmpty(searchText)) + { + listBoxSongList2.Visible = false; + listBoxSongList.Visible = true; + } + else + { + listBoxSongList.Visible = false; + listBoxSongList2.Visible = true; + } + //using binding source , if don't there's error happen + var songBinding = new BindingSource(); + var songList = Songs.Where(Q => Q.Title.ToLower().Contains(searchText) || Q.Artist.ToLower().Contains(searchText)); + foreach(var song in songList ) { + songBinding.Add(song); + } + listBoxSongList2.DataSource = songBinding; + listBoxSongList2.DisplayMember = "Title"; + } } } diff --git a/ExamNETIntermediate/Form1.resx b/ExamNETIntermediate/Form1.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ExamNETIntermediate/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ExamNETIntermediate/Song.cs b/ExamNETIntermediate/Song.cs new file mode 100644 index 0000000..292864f --- /dev/null +++ b/ExamNETIntermediate/Song.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace ExamNETIntermediate +{ + public class Song + { + public int SongId { get; set; } + public string Title { get; set; } = string.Empty; + + public string Artist { get; set; } = string.Empty; + public string GenreName { get; set; } = string.Empty; + public int Length { get; set; } + + public DateTimeOffset ReleaseDate { get; set; } + public bool IsAvailable { get; set; } = false; + } + + public class SongInput + { + public int SongId { get; set; } + public string Title { get; set; } = string.Empty; + + public string Artist { get; set; } = string.Empty; + public int GenreId { get; set; } + public int Length { get; set; } + + public DateTimeOffset ReleaseDate { get; set; } + public bool IsAvailable { get; set; } = false; + } + + public class Genre + { + public int GenreId { get; set; } + public string GenreName { get; set; } = string.Empty; + } +}