• Register
262 points
‚óŹ3

Before continue to this post, please check our Part A of this article.

Finally we have to design Presentation Tier. To do this:

  • Create a windows form application project or Windows Presentation Foundation (WPF).
    cd ..
    mkdir PresentationTier
    cd PresentationTier
    dotnet new winforms

     

  • Design a layout for CURD operation Winform design
     
  • Add required api callbacks on events of the form and it's components
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Net.Http.Json;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace PresentationTier
    {
        public partial class StudentForm : Form
        {
            private readonly HttpClient _httpClient;
            public StudentForm()
            {
                InitializeComponent();
                _httpClient = new HttpClient {BaseAddress = new Uri("http://localhost:5000/")};
                _httpClient.DefaultRequestHeaders.Accept.Clear();
                _httpClient.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
            }
    
            private async void Form1_Load(object sender, EventArgs e)
            {
                await RefreshStudentList();
            }
    
            private async Task AddStudent(Student student)
            {
                var response = await _httpClient.PostAsJsonAsync("api/Students", student);
                response.EnsureSuccessStatusCode();
                MessageBox.Show("Added Sucessfully.");
            }
            private async Task UpdateStudent(Student student)
            {
                var response = await _httpClient.PutAsJsonAsync($"api/Students/{student.Id}", student);
                response.EnsureSuccessStatusCode();
                MessageBox.Show("Updated Sucessfully.");
            }
            private async Task RemoveStudent(Guid studentId)
            {
    
                var confirmed =MessageBox.Show(
                    "Are you sure?", 
                    "This information will be deleted!", 
                    MessageBoxButtons.YesNo);
                if (confirmed == DialogResult.Yes)
                {
                    var response = await _httpClient.DeleteAsync($"api/Students/{studentId}");
                    response.EnsureSuccessStatusCode();
                    MessageBox.Show("Deleted Sucessfully.");
                }
                
            }
            private async Task RefreshStudentList()
            {
                studentsGrid.DataSource = await _httpClient
                    .GetFromJsonAsync<List<Student>>("api/Students");
            }
    
            private void studentsGrid_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (studentsGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                {
                    if (studentsGrid.CurrentRow != null) 
                        studentsGrid.CurrentRow.Selected = true;
                    studentId.Text = studentsGrid.Rows[e.RowIndex].Cells["Id"].FormattedValue.ToString();
                    firstName.Text = studentsGrid.Rows[e.RowIndex].Cells["FirstName"].FormattedValue.ToString();
                    lastName.Text = studentsGrid.Rows[e.RowIndex].Cells["LastName"].FormattedValue.ToString();
                    registrationNo.Text = studentsGrid.Rows[e.RowIndex].Cells["RegNo"].FormattedValue.ToString();
                }
            }
    
            private async void resetBtn_Click(object sender, EventArgs e)
            {
                await ResetForm();
            }
    
            private async Task ResetForm()
            {
                studentId.Text = Guid.Empty.ToString();
                firstName.Text = "";
                lastName.Text = "";
                registrationNo.Text = "";
                await RefreshStudentList();
            }
    
            private async void submitBtn_Click(object sender, EventArgs e)
            {
                if (firstName.Text.Trim().Length < 1 ||
                    lastName.Text.Trim().Length < 1 || registrationNo.Text.Length < 1)
                {
                    MessageBox.Show("Invalid Data!");
                }
                else
                {
    
                    var student = new Student
                    {
                        Id = Guid.Parse(studentId.Text),
                        FirstName = firstName.Text,
                        LastName = lastName.Text,
                        RegNo = Convert.ToInt32(registrationNo.Text)
                    };
                    if (student.Id == Guid.Empty)
                    {
                        await AddStudent(student);
                    }
                    else
                    {
                        await UpdateStudent(student);
                    }
    
                    await ResetForm();
                }
    
            }
    
            private async void deleteBtn_Click(object sender, EventArgs e)
            {
                await RemoveStudent(Guid.Parse(studentId.Text));
                await ResetForm();
            }
        }
    }
    

     

We have finished our development phases.Now run your application and test it's interractions and enjoy.

Windows forms Student