ASP.NET / ASP.NET Helpdesk System Using jQuery and Bootstrap / HelpdeskViewModels / ProblemViewModel.cs
ProblemViewModel.cs
Raw
/*
 * Description: The ProblemViewModel class is a view model for problems. The class has methods
 *              to retrieve a problem by its description and to retrieve all problems. These 
 *              methods use a ProblemDAO 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 ProblemViewModel
    {
        private readonly ProblemDAO _dao;
        public int? Id { get; set; }
        public string? Description { get; set; }
        public string? Timer { get; set; }
        // constructor
        public ProblemViewModel()
        {
            _dao = new ProblemDAO();
        }

        //
        // Retrieve Problem by description
        //
        public async Task GetByDescription()
        {
            try
            {
                Problem prob = await _dao.GetByDescription(Description!);
                Description = prob.Description;
                Id = prob.Id;
                Timer = Convert.ToBase64String(prob.Timer!);
            }
            catch (NullReferenceException nex)
            {
                Debug.WriteLine(nex.Message);
                Description = "not found";
            }
            catch (Exception ex)
            {
                Description = "not found";
                Debug.WriteLine("Problem in " + GetType().Name + " " +
                 MethodBase.GetCurrentMethod()!.Name + " " + ex.Message);
                throw;
            }
        }

        //
        // Retrieve all the Problems
        //
        public async Task<List<ProblemViewModel>> GetAll()
        {
            List<ProblemViewModel> allVms = new();
            try
            {
                List<Problem> allProblems = await _dao.GetAll();
                foreach (Problem prob in allProblems)
                {
                    ProblemViewModel probVm = new()
                    {
                        Description = prob.Description,
                        Id = prob.Id,
                        Timer = Convert.ToBase64String(prob.Timer!)
                    };

                    allVms.Add(probVm);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Problem in " + GetType().Name + " " +
                MethodBase.GetCurrentMethod()!.Name + " " + ex.Message);
                throw;
            }
            return allVms;
        }
    }
}