Home > other >  crud operation with dynamic data with 3 level hierarchy in json file in asp .net core web api
crud operation with dynamic data with 3 level hierarchy in json file in asp .net core web api

Time:05-20

I want to do get, post, put and delete in it using asp.net core Web API. But I should not use database to store the data instead I need to store the dynamic data in controller using method and also in json file list of user(userId,userName),3 level hierarchy(Country should have list of state,State should have list of city,And I could add Countries along with its states and cities) and use it to do the http action. Can any one please help me with some step or code?

CodePudding user response:

I create a sample demo for you, and I suggest you don't use json file.

Why:

  1. When you use json files, IO operations are required, and there will be a problem of exclusive use of files.

  2. In addition, when adding, deleting, modifying and checking the content of the file, every time there is a json file that is read, then converted into an object, and then written into the file, the efficiency will be very low.

Because what you want is a demo, the test code I wrote does not have any verification. You can pay attention to the places that need attention.

  1. AddSingleton must be used when registering a service, so as to ensure that all users access the same data source.

  2. When the amount of data is very large and there are too many requests, there will be a situation where the data does not match. Because I have not added any locks or restrictions here.

Test Result

enter image description here

Test Code:

  1. Create GlobalVariablesService, and register it

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace DB_Project.Models
    {
    
     public class GlobalVariablesService
     {
         public List<RecordModel> records = new List<RecordModel>();
         public GlobalVariablesService(){
    
         }
         public List<RecordModel> AddRecord(RecordModel r) {
             records.Add(r);
             return records;
         }
         public List<RecordModel> RemoveRecord(int rid)
         {
             var itemToRemove = records.Single(r => r.Record_ID == rid);
             records.Remove(itemToRemove);
             return records;
         }
         public RecordModel GetRecord(int rid)
         {
             var itemToGet = records.Single(r => r.Record_ID == rid);
             return itemToGet;
         }
    
     }
     /// <summary>
     /// Add Record History
     /// Rid  #Uid  #Country_id #Country_Name  #State_id   #State_Name  #City_id  #City_Name
     ///  1    001       C1         Country1         S1        State1       C_1       City1
     ///  2    002       C2         Country2         S2        State2       C_2       City2
     ///  3    003       C3         Country3         S3        State3       C_3       City3
     /// </summary>
     /// 
    
     public class RecordModel { 
         public int Record_ID { get; set; }
         public int Uid { get; set; }
         public string Country_id { set; get; }
         public string Country_Name { set; get; }
         public string State_id { set; get; }
         public string State_Name { set; get; }
         public string City_id { set; get; }
         public string City_Name { set; get; }
     }
    
     public class UserModel { 
         public int Uid { set; get; }
         public string Name { set; get; }
     }
     public class CountryModel
     {
    
         public string Country_id { set; get; }
         public string Country_Name { set; get; }
         public List<StateModel> State { set; get; }
     }
     public class StateModel
     {
         public string State_id { set; get; }
         public string State_Name { set; get; }
         public List<CityModel> City { set; get; }
     }
     public class CityModel
     {
         public string City_id { set; get; }
         public string City_Name { set; get; }
     }
    }
    
  2. Register service in Startup.cs file;

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddSingleton<GlobalVariablesService>();
    }
    
  3. My Test Controller

    using DB_Project.DbClass;
    using DB_Project.Models;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace DB_Project.Controllers
    {
     public class TestController : Controller
     {
         private readonly ILogger<TestController> _logger;
         private readonly GlobalVariablesService _service;
         public TestController(ILogger<TestController> logger, GlobalVariablesService service)
         {
             _logger = logger;
             _service = service;
         }
         public IActionResult get(int rid)
         {
             try
             {
                 var model = _service.GetRecord(rid);
                 return Ok(model);
             }
             catch (Exception e)
             {
                 return Ok("error occured :"   e.ToString());
                 throw;
             }
    
         }
         public string add(RecordModel r)
         {//string uid,string countryid,string countryname,string stateid,string statename,string cityid,string cityname ) {
             try
             {
                 _service.AddRecord(r);
                 return "success";
             }
             catch (Exception)
             {
                 return "failed";
                 throw;
             }
    
         }
         public string delete(int rid){
             try
             {
                 _service.RemoveRecord(rid);
                 return "success";
             }
             catch (Exception)
             {
                 return "failed";
                 throw;
             }
    
         }
     }
    }
    
  • Related