Esempio n. 1
0
        async Task GetCourses()
        {
            try
            {
                this.IsRefreshing = true;

                var connection = await courseService.CheckConnection();

                if (!connection)
                {
                    this.IsRefreshing = false;
                    await Application.Current.MainPage.DisplayAlert("Error", "No internet connection", "Cancel");

                    return;
                }

                var listCourses = (await courseService.GetAll(Endpoints.GET_COURSES)).Select(x => ToCourseItemViewModel(x));
                this.AllCourses   = listCourses.ToList();
                this.Courses      = new ObservableCollection <CourseItemViewModel>(listCourses);
                this.IsRefreshing = false;
            }
            catch (Exception ex)
            {
                this.IsRefreshing = false;
                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Cancel");
            }
        }
        async Task DeleteCourse()
        {
            var answer = await Application.Current.MainPage.DisplayAlert("Confirm", "Delete confirm", "Yes", "No");

            if (!answer)
            {
                return;
            }

            var connection = await courseService.CheckConnection();

            if (!connection)
            {
                await Application.Current.MainPage.DisplayAlert("Error", "No internet connection", "Cancel");

                return;
            }

            await courseService.Delete(Endpoints.DELETE_COURSES, this.CourseID);

            await Application.Current.MainPage.DisplayAlert("Message", "The process is successful", "Confirm");

            var courseViewModel = CoursesViewModel.GetInstance();
            var courseDeleted   = courseViewModel.AllCourses.FirstOrDefault(x => x.CourseID == this.CourseID);

            courseViewModel.AllCourses.Remove(courseDeleted);
            courseViewModel.GetCoursesByTitle();
        }
        async Task CreateCourse()
        {
            try
            {
                if (string.IsNullOrEmpty(this.Title))
                {
                    await Application.Current.MainPage.DisplayAlert("Error", "You must enter the field title", "Cancel");

                    return;
                }

                this.IsRunning = true;
                this.IsEnabled = false;

                var connection = await courseService.CheckConnection();

                if (!connection)
                {
                    this.IsRunning = false;
                    this.IsEnabled = true;

                    await Application.Current.MainPage.DisplayAlert("Error", "No internet connection", "Cancel");

                    return;
                }

                var courseDTO = new CourseDTO {
                    CourseID = this.CourseID, Title = this.Title, Credits = this.Credits
                };
                await courseService.Create(Endpoints.POST_COURSES, courseDTO);

                this.IsRunning = false;
                this.IsEnabled = true;

                await Application.Current.MainPage.DisplayAlert("Message", "The process is successful", "Cancel");

                this.CourseID = this.Credits = 0;
                this.Title    = string.Empty;

                Application.Current.MainPage = new NavigationPage(new CoursesPage());
                //var enrollmentDate = DateTime.UtcNow;
                //this.EnrollmentDate = DateTime.UtcNow;
            }
            catch (Exception ex)
            {
                this.IsRunning = false;
                this.IsEnabled = true;

                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Cancel");
            }
        }
        async Task EditCourse()
        {
            try
            {
                if (string.IsNullOrEmpty(this.Course.Title))
                {
                    await Application.Current.MainPage.DisplayAlert("Error", "You must enter the title field", "Cancel");

                    return;
                }

                this.IsRunning = true;
                this.IsEnabled = false;

                var connection = await courseService.CheckConnection();

                if (!connection)
                {
                    this.IsRunning = false;
                    this.IsEnabled = true;

                    await Application.Current.MainPage.DisplayAlert("Error", "No internet connection", "Cancel");

                    return;
                }

                var courseDTO = new CourseDTO {
                    CourseID = this.Course.CourseID, Title = this.Course.Title, Credits = this.Course.Credits
                };
                await courseService.Update(Endpoints.PUT_COURSES, this.Course.CourseID, courseDTO);

                this.IsRunning = false;
                this.IsEnabled = true;

                await Application.Current.MainPage.DisplayAlert("Message", "The process is successful", "Confirm");

                this.Course.CourseID = this.Course.Credits = 0;
                this.Course.Title    = string.Empty;

                await Application.Current.MainPage.Navigation.PopAsync();
            }
            catch (Exception ex)
            {
                this.IsRunning = false;
                this.IsEnabled = true;

                await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Cancel");
            }
        }