Design

Design
asp.net mvc

2018年6月25日 星期一

懶得用DB 那就建個.json資料 來webapi CRUD吧

有時候懶得搞DB 這時候我們就可以本地建個JSON檔或是XML檔來當做資料庫
當然我不推崇這種方法 因為這會有很多問題 一方面效能 一方面資安 等等各種問題

那至於該怎做呢 方便我們前端呼叫做存取 這邊用webapi 當作範例
這邊依照飲料做為範例 先定義我們的Model格式吧





public class Type
    {
        public string Size { get; set; }
        public int Price { get; set; }
    }

    public class Drink
    {
        public int Drinkid { get; set; }
        public string DrinkName { get; set; }
        public List type { get; set; }
        public string Remarks { get; set; }
    }

    public class Blend
    {
        public string blendName { get; set; }
        public int Price { get; set; }
    }

    public class StoreModel
    {
        public int id { get; set; }
        public string StoreName { get; set; }
        public string Url { get; set; }
        public List Drink { get; set; }
        public List blend { get; set; }
        public string StoreRemarks { get; set; }
    }

再來就是簡單的CRUD 這邊自行看看吧 這邊都是邏輯都很簡單都是一照我們ID去找我們的資料再來重新復寫我們的資料

 public class StoreController : ApiController
    {

        [AllowAnonymous]
        [HttpPost]
        public List GetStore()
        {
            var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Store/Store.json");

            return JsonConvert.DeserializeObject>(File.ReadAllText(mappedPath));

        }

        [AllowAnonymous]
        [HttpPost]
        public StoreModel GetStorebyID(int id)
        {
            var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Store/Store.json");
            using (StreamReader r = new StreamReader(mappedPath))
            {
                string json = r.ReadToEnd();
                return JsonConvert.DeserializeObject>(json).Find(x => x.id == id);
            }
        }


        [AllowAnonymous]
        [HttpPost]
        public bool DeleteById(int id)
        {
            try
            {
                var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Store/Store.json");
                var json = JsonConvert.DeserializeObject>(File.ReadAllText(mappedPath)).Find(x => x.id != id);
                var list = new List();
                list.Add(json);
                File.WriteAllText(mappedPath, JsonConvert.SerializeObject(list));
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        [AllowAnonymous]
        [HttpPost]
        public bool Add(StoreModel StoreModel)
        {
            try
            {
                var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Store/Store.json");
                var json = JsonConvert.DeserializeObject>(File.ReadAllText(mappedPath));
                json.Add(StoreModel);
                File.WriteAllText(mappedPath, JsonConvert.SerializeObject(json));
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

    }

這邊就簡單做個紀錄

沒有留言:

張貼留言