ASP.NET / ASP.NET Helpdesk System Using jQuery and Bootstrap / HelpdeskViewModels / DepartmentViewModel.cs
DepartmentViewModel.cs
Raw
/*
 * Description: The DepartmentViewModel class is a view model for departments. The class has
 *              a method to retrieve all departments and uses a DepartmentDAO object to
 *              access data from the database.
 */
using HelpdeskDAL;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
namespace HelpdeskViewModels
{
    public class DepartmentViewModel
    {
        private readonly DepartmentDAO _dao;
        public string? Timer { get; set; }
        public string? DepartmentName { get; set; }
        public int? Id { get; set; }

        // constructor
        public DepartmentViewModel()
        {
            _dao = new DepartmentDAO();
        }

        //
        // Retrieve all the Departments as ViewModel instances
        //
        public async Task<List<DepartmentViewModel>> GetAll()
        {
            List<DepartmentViewModel> allVms = new();
            try
            {
                List<Department> allDepartments = await _dao.GetAll();
                foreach (Department dep in allDepartments)
                {
                    DepartmentViewModel depVm = new()
                    {
                        DepartmentName = dep.DepartmentName,
                        Id = dep.Id,
                        Timer = Convert.ToBase64String(dep.Timer!)
                    };
                    allVms.Add(depVm);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Problem in " + GetType().Name + " " +
                MethodBase.GetCurrentMethod()!.Name + " " + ex.Message);
                throw;
            }
            return allVms;
        }
    }
}