Example #1
0
        public async Task OnPostAsync()
        {
            try
            {
                Connection.Open();

                ExceptionMessage = null;

                Objects.Clear();

                ResultMessage = "Object was updated successfully";

                switch (SelectedInfoType)
                {
                case null:
                    throw new Exception("type of information isn't chosed");

                case "student":
                    if (IsAnyVisibleInputFieldNotFilled(6))
                    {
                        throw new Exception("all input fields must be filled (first field with non-zero value)");
                    }

                    SecondInputField = SecondInputField.Trim();
                    ThirdInputField  = ThirdInputField.Trim();
                    FourthInputField = FourthInputField.Trim();
                    FifthInputField  = FifthInputField.Trim();
                    SixthInputField  = SixthInputField.Trim();

                    if (!IsValidFirstOrLastName(SecondInputField))
                    {
                        throw new Exception("incorrect first name");
                    }

                    if (!IsValidFirstOrLastName(ThirdInputField))
                    {
                        throw new Exception("incorrect last name");
                    }

                    if (!IsValidPhoneNumber(FourthInputField))
                    {
                        throw new Exception("incorrect phone number");
                    }

                    if (!IsValidEmailAddress(FifthInputField))
                    {
                        throw new Exception("incorrect email address");
                    }

                    var studentToUpdate = new Student
                    {
                        StudentID   = FirstInputField,
                        FirstName   = SecondInputField,
                        LastName    = ThirdInputField,
                        PhoneNumber = FourthInputField,
                        Email       = FifthInputField,
                        Github      = SixthInputField
                    };

                    var numberOfAffectedStudentRows = await Repository.UpdateStudentAsync(Connection, studentToUpdate);

                    if (numberOfAffectedStudentRows == 0)
                    {
                        ResultMessage = "There isn't student with such ID in database";
                    }
                    else
                    {
                        Objects.AddRange(await Repository.GetAllStudentsAsync(Connection));
                    }
                    break;

                case "course":
                    if (IsAnyVisibleInputFieldNotFilled(6))
                    {
                        throw new Exception("all input fields must be filled (first field with non-zero value)");
                    }

                    SecondInputField = SecondInputField.Trim();
                    ThirdInputField  = ThirdInputField.Trim();
                    FourthInputField = FourthInputField.Trim();
                    FifthInputField  = FifthInputField.Trim();

                    if (!ValidateDateTimeAndGetParsed(ThirdInputField, out DateTime resultStartDate))
                    {
                        throw new Exception("incorrect start date");
                    }

                    if (!ValidateDateTimeAndGetParsed(FourthInputField, out DateTime resultEndDate))
                    {
                        throw new Exception("incorrect end date");
                    }

                    if (!ValidateIntAndGetParsed(FifthInputField, out int resultPassingScore))
                    {
                        throw new Exception("incorrect passing score");
                    }

                    var courseToUpdate = new Course
                    {
                        CourseID     = FirstInputField,
                        Name         = SecondInputField,
                        StartDate    = resultStartDate,
                        EndDate      = resultEndDate,
                        PassingScore = resultPassingScore
                    };

                    var numberOfAffectedCourseRows = await Repository.UpdateCourseAsync(Connection, courseToUpdate);

                    if (numberOfAffectedCourseRows == 0)
                    {
                        ResultMessage = "There isn't course with such ID in database";
                    }
                    else
                    {
                        Objects.AddRange(await Repository.GetAllCoursesAsync(Connection));
                    }
                    break;

                case "lecturer":
                    if (IsAnyVisibleInputFieldNotFilled(3))
                    {
                        throw new Exception("all input fields must be filled (first field with non-zero value)");
                    }

                    SecondInputField = SecondInputField.Trim();
                    ThirdInputField  = ThirdInputField.Trim();

                    if (!IsValidFirstOrLastName(SecondInputField))
                    {
                        throw new Exception("incorrect name");
                    }

                    if (!ValidateDateTimeAndGetParsed(ThirdInputField, out DateTime resultBirthDate))
                    {
                        throw new Exception("incorrect birth date");
                    }

                    var lecturerToUpdate = new Lecturer
                    {
                        LecturerID = FirstInputField,
                        Name       = SecondInputField,
                        BirthDate  = resultBirthDate
                    };

                    var numberOfAffectedLecturerRows = await Repository.UpdateLecturerAsync(Connection, lecturerToUpdate);

                    if (numberOfAffectedLecturerRows == 0)
                    {
                        ResultMessage = "There isn't lecturer with such ID in database";
                    }
                    else
                    {
                        Objects.AddRange(await Repository.GetAllLecturersAsync(Connection));
                    }
                    break;

                case "hometask":
                    if (IsAnyVisibleInputFieldNotFilled(6))
                    {
                        throw new Exception("all input fields must be filled (first field with non-zero value)");
                    }

                    SecondInputField = SecondInputField.Trim();
                    ThirdInputField  = ThirdInputField.Trim();
                    FourthInputField = FourthInputField.Trim();
                    FifthInputField  = FifthInputField.Trim();
                    SixthInputField  = SixthInputField.Trim();

                    if (!ValidateDateTimeAndGetParsed(FourthInputField, out DateTime resultTaskDate))
                    {
                        throw new Exception("incorrect task date");
                    }

                    if (!ValidateIntAndGetParsed(FifthInputField, out int resultSerialNumber))
                    {
                        throw new Exception("incorrect serial number");
                    }

                    if (!ValidateIntAndGetParsed(SixthInputField, out int resultCourseID))
                    {
                        throw new Exception("incorrect course ID input number");
                    }

                    var hometaskToUpdate = new HomeTask
                    {
                        HomeTaskID   = FirstInputField,
                        Name         = SecondInputField,
                        Description  = ThirdInputField,
                        TaskDate     = resultTaskDate,
                        SerialNumber = resultSerialNumber,
                        CourseID     = resultCourseID
                    };

                    var numberOfAffectedHometaskRows = await Repository.UpdateHomeTaskAsync(Connection, hometaskToUpdate);

                    if (numberOfAffectedHometaskRows == 0)
                    {
                        ResultMessage = "There isn't hometask with such ID in database";
                    }
                    else
                    {
                        Objects.AddRange(await Repository.GetAllHomeTasksAsync(Connection));
                    }
                    break;

                case "grade":
                    if (IsAnyVisibleInputFieldNotFilled(5))
                    {
                        throw new Exception("all input fields must be filled (first field with non-zero value)");
                    }

                    SecondInputField = SecondInputField.Trim();
                    ThirdInputField  = ThirdInputField.Trim();
                    FourthInputField = FourthInputField.Trim();
                    FifthInputField  = FifthInputField.Trim();

                    if (!ValidateDateTimeAndGetParsed(SecondInputField, out DateTime resultGradeDate))
                    {
                        throw new Exception("incorrect grade date");
                    }

                    if (!ValidateBoolAndGetParsed(ThirdInputField, out bool resultIsComplete))
                    {
                        throw new Exception("incorrect is complete");
                    }

                    if (!ValidateIntAndGetParsed(FourthInputField, out int resultHometaskID))
                    {
                        throw new Exception("incorrect hometask ID input number");
                    }

                    if (!ValidateIntAndGetParsed(FifthInputField, out int resultStudentID))
                    {
                        throw new Exception("incorrect student ID input number");
                    }

                    var gradeToUpdate = new Grade
                    {
                        GradeDate  = resultGradeDate,
                        IsComplete = resultIsComplete,
                        HomeTaskID = resultHometaskID,
                        StudentID  = resultStudentID
                    };

                    var numberOfAffectedGradeRows = await Repository.UpdateGradeAsync(Connection, gradeToUpdate);

                    if (numberOfAffectedGradeRows == 0)
                    {
                        ResultMessage = "There isn't grade with such ID in database";
                    }
                    else
                    {
                        Objects.AddRange(await Repository.GetAllGradesAsync(Connection));
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                ExceptionMessage = "Error: " + ex.Message;
            }
            finally
            {
                Connection.Close();
            }
        }
Example #2
0
        public async Task OnPostAsync()
        {
            try
            {
                Connection.Open();

                ExceptionMessage = null;

                Objects.Clear();

                switch (SelectedInfoType)
                {
                case null:
                    throw new Exception("type of information isn't chosed");

                case "student":
                    if (IsAnyVisibleInputFieldNull(5))
                    {
                        throw new Exception("all input fields must be filled");
                    }

                    FirstInputField  = FirstInputField.Trim();
                    SecondInputField = SecondInputField.Trim();
                    ThirdInputField  = ThirdInputField.Trim();
                    FourthInputField = FourthInputField.Trim();
                    FifthInputField  = FifthInputField.Trim();

                    if (!IsValidFirstOrLastName(FirstInputField))
                    {
                        throw new Exception("incorrect first name");
                    }

                    if (!IsValidFirstOrLastName(SecondInputField))
                    {
                        throw new Exception("incorrect last name");
                    }

                    if (!IsValidPhoneNumber(ThirdInputField))
                    {
                        throw new Exception("incorrect phone number");
                    }

                    if (!IsValidEmailAddress(FourthInputField))
                    {
                        throw new Exception("incorrect email address");
                    }

                    var newStudent = new Student
                    {
                        FirstName   = FirstInputField,
                        LastName    = SecondInputField,
                        PhoneNumber = ThirdInputField,
                        Email       = FourthInputField,
                        Github      = FifthInputField
                    };

                    await Repository.CreateStudentAsync(Connection, newStudent);

                    Objects.AddRange(await Repository.GetAllStudentsAsync(Connection));
                    break;

                case "course":
                    if (IsAnyVisibleInputFieldNull(4))
                    {
                        throw new Exception("all input fields must be filled");
                    }

                    FirstInputField  = FirstInputField.Trim();
                    SecondInputField = SecondInputField.Trim();
                    ThirdInputField  = ThirdInputField.Trim();
                    FourthInputField = FourthInputField.Trim();

                    if (!ValidateDateTimeAndGetParsed(SecondInputField, out DateTime resultStartDate))
                    {
                        throw new Exception("incorrect start date");
                    }

                    if (!ValidateDateTimeAndGetParsed(ThirdInputField, out DateTime resultEndDate))
                    {
                        throw new Exception("incorrect end date");
                    }

                    if (!ValidateIntAndGetParsed(FourthInputField, out int resultPassingScore))
                    {
                        throw new Exception("incorrect passing score");
                    }

                    var newCourse = new Course
                    {
                        Name         = FirstInputField,
                        StartDate    = resultStartDate,
                        EndDate      = resultEndDate,
                        PassingScore = resultPassingScore
                    };

                    await Repository.CreateCourseAsync(Connection, newCourse);

                    Objects.AddRange(await Repository.GetAllCoursesAsync(Connection));
                    break;

                case "lecturer":
                    if (IsAnyVisibleInputFieldNull(2))
                    {
                        throw new Exception("all input fields must be filled");
                    }

                    FirstInputField  = FirstInputField.Trim();
                    SecondInputField = SecondInputField.Trim();

                    if (!IsValidFirstOrLastName(FirstInputField))
                    {
                        throw new Exception("incorrect name");
                    }

                    if (!ValidateDateTimeAndGetParsed(SecondInputField, out DateTime resultBirthDate))
                    {
                        throw new Exception("incorrect birth date");
                    }

                    var newLecturer = new Lecturer
                    {
                        Name      = FirstInputField,
                        BirthDate = resultBirthDate
                    };

                    await Repository.CreateLecturerAsync(Connection, newLecturer);

                    Objects.AddRange(await Repository.GetAllLecturersAsync(Connection));
                    break;

                case "hometask":
                    if (IsAnyVisibleInputFieldNull(5))
                    {
                        throw new Exception("all input fields must be filled");
                    }

                    FirstInputField  = FirstInputField.Trim();
                    SecondInputField = SecondInputField.Trim();
                    ThirdInputField  = ThirdInputField.Trim();
                    FourthInputField = FourthInputField.Trim();
                    FifthInputField  = FifthInputField.Trim();

                    if (!ValidateDateTimeAndGetParsed(ThirdInputField, out DateTime resultTaskDate))
                    {
                        throw new Exception("incorrect task date");
                    }

                    if (!ValidateIntAndGetParsed(FourthInputField, out int resultSerialNumber))
                    {
                        throw new Exception("incorrect serial number");
                    }

                    if (!ValidateIntAndGetParsed(FifthInputField, out int resultCourseID))
                    {
                        throw new Exception("incorrect course ID input number");
                    }

                    var newHometask = new HomeTask
                    {
                        Name         = FirstInputField,
                        Description  = SecondInputField,
                        TaskDate     = resultTaskDate,
                        SerialNumber = resultSerialNumber,
                        CourseID     = resultCourseID
                    };

                    await Repository.CreateHomeTaskAsync(Connection, newHometask);

                    Objects.AddRange(await Repository.GetAllHomeTasksAsync(Connection));
                    break;

                case "grade":
                    if (IsAnyVisibleInputFieldNull(4))
                    {
                        throw new Exception("all input fields must be filled");
                    }

                    FirstInputField  = FirstInputField.Trim();
                    SecondInputField = SecondInputField.Trim();
                    ThirdInputField  = ThirdInputField.Trim();
                    FourthInputField = FourthInputField.Trim();

                    if (!ValidateDateTimeAndGetParsed(FirstInputField, out DateTime resultGradeDate))
                    {
                        throw new Exception("incorrect grade date");
                    }

                    if (!ValidateBoolAndGetParsed(SecondInputField, out bool resultIsComplete))
                    {
                        throw new Exception("incorrect is complete");
                    }

                    if (!ValidateIntAndGetParsed(ThirdInputField, out int resultHometaskID))
                    {
                        throw new Exception("incorrect hometask ID input number");
                    }

                    if (!ValidateIntAndGetParsed(FourthInputField, out int resultStudentID))
                    {
                        throw new Exception("incorrect student ID input number");
                    }

                    var newGrade = new Grade
                    {
                        GradeDate  = resultGradeDate,
                        IsComplete = resultIsComplete,
                        HomeTaskID = resultHometaskID,
                        StudentID  = resultStudentID
                    };

                    await Repository.CreateGradeAsync(Connection, newGrade);

                    Objects.AddRange(await Repository.GetAllGradesAsync(Connection));
                    break;
                }
            }
            catch (Exception ex)
            {
                ExceptionMessage = "Error: " + ex.Message;
            }
            finally
            {
                Connection.Close();
            }
        }