<repo_name>rahul28malviya/TutorWebApp<file_sep>/README.md
# TutorWebApp
I have created a sample web application named as TutorWebApp in C# along with AngularJS, using .NET framework 4.7.2 and Angular1.7. This application has the following features.
Bind the data and showing in table list (UI).
Create, Edit and Update tutors.
In Memory Data storage.
<file_sep>/TutorWebApp/Controllers/HomeController.cs
﻿using System.Collections.Generic;
using System.Web.Mvc;
using TutorWebApp.InMemoryData;
using TutorWebApp.Models;

namespace TutorWebApp.Controllers
{
    public class HomeController : Controller
    {
        private static int Id;

        static HomeController()
        {
            Id = 1;
            //Adding Intial Record
            CacheStore.Add(Id.ToString(), new Tutor { Id = 1, Name = "<NAME>", Email = "<EMAIL>", Topics = "AngularJS Assignment", Price = 1000 });
        }

        public ActionResult Index()
        {
            return View();
        }

        //public ActionResult About()
        //{
        //    ViewBag.Message = "Your application description page.";

        //    return View();
        //}

        //public ActionResult Contact()
        //{
        //    ViewBag.Message = "Your contact page.";

        //    return View();
        //}

        [HttpPost]
        public JsonResult AddEditDetails(Tutor model)
        {
            string status = string.Empty;
            if (ModelState.IsValid)
            {
                if (model.Id > 0)
                {
                    CacheStore.Remove<Tutor>(model.Id.ToString());
                    CacheStore.Add<Tutor>(model.Id.ToString(), model);
                    status = "updated";
                }
                else
                {
                    model.Id = ++Id;
                    CacheStore.Add(Id.ToString(), model);
                    status = "saved";
                }
            }
            string message = $"Tutor has been { status } successfully!";
            return Json(message, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult Delete(int id)
        {
            CacheStore.Remove<Tutor>(id.ToString());
            string message = $"User has been removed successfully!";
            return Json(message, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetAll()
        {
            List<Tutor> tutors = CacheStore.GetAll<Tutor>();
            return Json(tutors, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult Get(int id)
        {
            Tutor result = CacheStore.Get<Tutor>(id.ToString());
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}<file_sep>/TutorWebApp/DataContext/InMemoryData.cs
﻿namespace TutorWebApp.InMemoryData
{
    using System;
    using System.Collections.Generic;

    public static class CacheStore
    {
        private static Dictionary<string, object> _cache;

        private static object _sync;

        static CacheStore()
        {
            _cache = new Dictionary<string, object>();
            _sync = new object();
        }
        public static bool Exists<T>(string key) where T : class
        {
            Type type = typeof(T);

            lock (_sync)
            {
                return _cache.ContainsKey(key);
            }
        }

        public static T Get<T>(string key) where T : class
        {
            Type type = typeof(T);

            lock (_sync)
            {
                if (_cache.ContainsKey(key) == false)
                    throw new ApplicationException(String.Format("An object with key '{0}' does not exists", key));

                lock (_sync)
                {
                    return (T)_cache[key];
                }
            }
        }

        public static List<T> GetAll<T>() where T : class
        {
            Type type = typeof(T);
            List<T> list = new List<T>();

            lock (_sync)
            {
                lock (_sync)
                {
                    foreach (KeyValuePair<string, object> temp in _cache)
                    {
                        list.Add((T)temp.Value);
                    }
                }
            }

            return list;
        }

        public static void Add<T>(string key, T value)
        {
            Type type = typeof(T);

            if (value.GetType() != type)
                throw new ApplicationException(String.Format("The type of value passed to cache {0} does not match the cache type {1} for key {2}", value.GetType().FullName, type.FullName, key));

            lock (_sync)
            {
                if (_cache.ContainsKey(key))
                    throw new ApplicationException(String.Format("An object with key '{0}' already exists", key));

                lock (_sync)
                {
                    _cache.Add(key, value);
                };
            }
        }

        public static void Remove<T>(string key)
        {
            Type type = typeof(T);

            lock (_sync)
            {
                if (_cache.ContainsKey(key) == false)
                    throw new ApplicationException(String.Format("An object with key '{0}' does not exists in cache", key));

                lock (_sync)
                {
                    _cache.Remove(key);
                }
            }
        }
    }
}