private async void bunifuThinButton3_Click(object sender, EventArgs e) { string name = nameTextBox.Text; string path = fileTextBox.Text; string error = ""; string specialChar = @"\|!#$%&/()=?»«@£§€{}.-;'<>_,"; foreach (var item in specialChar) { if (name.Contains(item)) { MessageBox.Show("Please enter a name without special characters.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //simple validation to verify against name - prevents clustering when adding larger classes if (name.Length < 3) { error = "Name is too short."; } else if (!File.Exists(path)) { error = "Specified file does not exist."; } if (error != "") { MessageBox.Show(error, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //debugging, shows user error return; } try { string url = await ApiWrapper.UploadFile(path); if (!await ApiWrapper.CreateHomework(classObj.Id, name, url)) //attempts to create homework { MessageBox.Show("Failed to create homework.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //throws error if anything failed } else { //homework was created Close(); } } catch { MessageBox.Show("It seem's the file is open, close it and try again..."); //likely scanerio when attaching files. } }
//submit homework private async void bunifuThinButton3_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 1) //ensures that student has an entity to where they will send homework too. { var item = listView1.SelectedItems[0]; var hw = (Homework)item.Tag; OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { string url = await ApiWrapper.UploadFile(ofd.FileName); //url contains name of file, easy referencing when looking for it manually await ApiWrapper.SubmitHomework(hw.Id, url); } } else if (listView1.SelectedItems.Count == 0) //error handling { MessageBox.Show("Please select an item from the list!", "No item selected", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Please select only one item!", "Multiple items selected", MessageBoxButtons.OK, MessageBoxIcon.Information); } }