예제 #1
0
        public IHttpActionResult Post(ToDoItemApiModel model)
        {
            var items   = _siteJsonDataHelper.LoadToDoItemsFromAppData();
            var newItem = Mapper.Map <ToDoItem>(model);

            newItem.Id     = items.Count + 1;
            newItem.Status = ToDoItemStatus.New;
            items.Add(newItem);
            var jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(items, Newtonsoft.Json.Formatting.Indented);

            File.WriteAllText(_siteJsonDataHelper.AppDataToDoItemsJsonFilePath, jsonResult, System.Text.Encoding.Default);
            return(Ok());
        }
예제 #2
0
        public IHttpActionResult Put(ToDoItemApiModel model)
        {
            var items    = _siteJsonDataHelper.LoadToDoItemsFromAppData();
            var toUpdate = items.FirstOrDefault(x => x.Id == model.Id);

            if (toUpdate != null)
            {
                var mapped = Mapper.Map <ToDoItem>(model);
                Mapper.Map(mapped, toUpdate);
                toUpdate.UpdatedOn = DateTime.Now;

                var jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(items, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText(_siteJsonDataHelper.AppDataToDoItemsJsonFilePath, jsonResult, System.Text.Encoding.Default);
                return(Ok());
            }
            return(BadRequest());
        }