更新MyToDo.Api/Service/ToDoService.cs
public async Task AddAsync(Todo model)
{
try
{
var todo = mapper.Map(model);
await work.GetRepository().InsertAsync(todo);
if (await work.SaveChangesAsync() > 0)
return new ApiReponse(true, todo);
return new ApiReponse(false);
}
catch (Exception ex)
{
return new ApiReponse(false, ex);
}
}
更新MyToDo.Api/Service/MemoService.cs
public async Task AddAsync(Memo model)
{
try
{
var memo = mapper.Map(model);
await work.GetRepository().InsertAsync(memo);
if (await work.SaveChangesAsync() > 0)
return new ApiReponse(true, memo);
return new ApiReponse(false);
}
catch (Exception ex)
{
return new ApiReponse(false, ex);
}
}
更新MyToDo.Api/Service/TodoView.xaml
更新MyToDo.Api/Service/Baservice.cs
public async Task DeleteAsync(int id)
{
BaseRequest request = new BaseRequest();
request.Method = RestSharp.Method.DELETE;
request.Route = $"api/{ServiceName}/Delete?todoid={id}";
return await client.ExecuteAsync(request);
}
更新文件:MyToDo/ViewModel/TodoViewModel.cs
添加内容:
///
/// 删除项
///
public DelegateCommand DeleteCommand { get; set; }
///
/// 删除指定项
///
///
async private void DeleteItem(ToDoDto dto)
{
var delres = await service.DeleteAsync(dto.Id);
if (delres.Status)
{
var model = TodoDtos.FirstOrDefault(t => t.Id.Equals(dto.Id));
TodoDtos.Remove(dto);
}
}
更新内容
public TodoViewModel(ITodoService service,IContainerProvider provider) : base(provider)
{
//初始化对象
TodoDtos = new ObservableCollection();
RightContentTitle = "添加血雨待办";
//初始化命令
SelectedCommand = new DelegateCommand(Selected);
OpenRightContentCmd = new DelegateCommand(Add);
ExecuteCommand = new DelegateCommand(ExceuteCmd);
DeleteCommand = new DelegateCommand(DeleteItem);
this.service = service;
}
更新文件:MyToDo/Views/TodoView.cs
更新内容:
更新文件:MyToDo.Api/Controllers/TodoController.cs
更新内容:
public async Task Delete(int todoid)=> await service.DeleteAsync(todoid);
添加文件:MyToDo/Common/Converters/IntToVisibilityConveter.cs
更新内容:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
namespace Mytodo.Common.Converters
{
[ValueConversion(typeof(Color), typeof(Brush))]
public class ColorToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Color color)
{
return new SolidColorBrush(color);
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is SolidColorBrush brush)
{
return brush.Color;
}
return default(Color);
}
}
}
更新文件:MyToDo/Views/Converters/TodoView.xaml.cs
更新内容:
xmlns:cv="clr-namespace:Mytodo.Common.Converters"
FontSize="14" />
using Mytodo.Common.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyToDo.Share.Models;
using MyToDo.Share.Contact;
using MyToDo.Share.Parameters;
using MyToDo.Share;
namespace Mytodo.Service
{
public interface ITodoService:IBaseService
{
Task>> GetAllFilterAsync(TodoParameter parameter);
}
}
MyToDo.Api.Controllers/TodoController
[HttpGet]
public async Task GetAll([FromQuery] TodoParameter param) => await service.GetAllAsync(param);
MyToDo.Api.Service/IToDoService
namespace MyToDo.Api.Service
{
public interface IToDoService : IBaseService
{
Task GetAllAsync(TodoParameter parameter);
}
}
更新文件:Mytodo.ViewModels/TodoViewModel.cs
///
/// 项目状态
///
public int SelectIndex
{
get { return selectIndex; }
set { selectIndex = value; RaisePropertyChanged(); }
}
private int selectIndex;
///
/// 保存消息
///
private async void Save()
{
try
{
if (string.IsNullOrWhiteSpace(CurrDto.Title) || string.IsNullOrWhiteSpace(CurrDto.Content))
return;
UpdateLoding(true);
if(CurrDto.Id>0) //编辑项
{
var updateres = await service.UpdateAsync(CurrDto);
if (updateres.Status)
{
UpdateDataAsync();
}
else
{
MessageBox.Show("更新失败");
}
}
else
{
//添加项
var add_res = await service.AddAsync(CurrDto);
//刷新
if (add_res.Status) //如果添加成功
{
TodoDtos.Add(add_res.Result);
}
else
{
MessageBox.Show("添加失败");
}
}
}
catch
{
}
finally
{
IsRightOpen = false;
//卸载数据加载窗体
UpdateLoding(false);
}
}
///
/// 打开待办事项弹窗
///
void Add()
{
CurrDto = new ToDoDto();
IsRightOpen = true;
}
private void Query()
{
GetDataAsync();
}
///
/// 根据条件更新数据
///
async void UpdateDataAsync()
{
int? Status = SelectIndex == 0 ? null : SelectIndex == 2 ? 1 : 0;
var todoResult = await service.GetAllFilterAsync(new MyToDo.Share.Parameters.TodoParameter { PageIndex = 0, PageSize = 100, Search = SearchString, Status = Status });
if (todoResult.Status)
{
todoDtos.Clear();
foreach (var item in todoResult.Result.Items)
todoDtos.Add(item);
}
}
///
/// 获取所有数据
///
async void GetDataAsync()
{
//调用数据加载页面
UpdateLoding(true);
//更新数据
UpdateDataAsync();
//卸载数据加载页面
UpdateLoding(false);
}
public async Task GetAllAsync(QueryParameter parameter)
{
try
{
var repository = work.GetRepository();
var todos = await repository.GetPagedListAsync(predicate:
x => string.IsNullOrWhiteSpace(parameter.Search) ? true : x.Title.Contains(parameter.Search),
pageIndex: parameter.PageIndex,
pageSize: parameter.PageSize,
orderBy: source => source.OrderByDescending(t => t.CreateDate));
return new ApiReponse(true, todos);
}
catch (Exception ex)
{
return new ApiReponse(ex.Message,false);
}
}
public async Task UpdateAsync(Todo model)
{
try
{
var dbtodo = mapper.Map(model);
//获取数据
var resposity = work.GetRepository();
//
var todo = await resposity.GetFirstOrDefaultAsync(predicate: x => x.Id.Equals(dbtodo.Id));
if(todo == null)
return new ApiReponse("修改失败,数据库中无给定条件的数据项",false);
todo.Title= dbtodo.Title;
todo.UpdateDate=DateTime.Now;
todo.CreateDate = dbtodo.CreateDate;
todo.Content = dbtodo.Content;
todo.Status = dbtodo.Status;
resposity.Update(todo);
if (await work.SaveChangesAsync() > 0)
return new ApiReponse(true);
return new ApiReponse(false);
}
catch (Exception ex)
{
return new ApiReponse(ex.Message, false);
}
}
Mytodo.Service/TodoService.cs
public async Task>> GetAllFilterAsync(TodoParameter parameter)
{
BaseRequest request = new BaseRequest();
request.Method = RestSharp.Method.GET;
var parameter_search = parameter.Search;
if(parameter_search==null)
{
request.Route = $"api/ToDo/GetAll?pageIndex={parameter.PageIndex}" +
$"&pageSize={parameter.PageSize}" +
$"&status={parameter.Status}";
}
else
request.Route = $"api/ToDo/GetAll?pageIndex={parameter.PageIndex}" +
$"&pageSize={parameter.PageSize}" +
$"&search={parameter.Search}" +
$"&status={parameter.Status}";
return await client.ExecuteAsync>(request);
}