Пример #1
0
        public async Task <ResultModel> ChangeToDoListItem(ToDoListItemForEdit model)
        {
            ResultModel result    = new ResultModel();
            Uri         address   = new Uri(_config["api"] + "/ToDoList");
            string      targetUri = address.ToString() + "/?";
            var         data      = JsonConvert.SerializeObject(model);

            result = await _callApi.CallAPI(data, new Uri(targetUri), "PUT");

            return(result);
        }
Пример #2
0
        public async Task <ResultModel> Edit(ToDoListItemForEdit model)
        {
            var result    = new ResultModel();
            var existData = _context.ToDoLists.Find(model.Id);

            if (existData == null)
            {
                result.IsSuccess = false;
                result.Message   = "Data is not found";
                return(result);
            }


            foreach (var property1 in model.GetType().GetProperties())
            {
                foreach (var property2 in existData.GetType().GetProperties())
                {
                    if (property1.Name == property2.Name)
                    {
                        if (property1.GetValue(model) != null)
                        {
                            property2.SetValue(existData, property1.GetValue(model));
                        }
                    }
                }
            }
            existData.EditDate = DateTime.Now;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message   = e.Message;
                return(result);
            }

            result.IsSuccess = true;
            result.Message   = "編輯成功";
            return(result);
        }