using detect.gui.Models.Entities;
using detect.gui.Services.Detect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Splat;

namespace detect.gui.Api.System;

public class UserApi
{
    public UserApi(IEndpointRouteBuilder? webApp)
    {
        // 用户登录
        webApp?.MapPost("/v1/system/user/login", ([FromBody] UserEntity user) => Locator.Current.GetService<DetectUserService>()!.Login(user.Username, user.Password));

        // 用户列表(所有)
        webApp?.MapGet("/v1/system/user/all", () => Locator.Current.GetService<DetectUserService>()!.ListAll());
        
        // Search
        webApp?.MapGet("/v1/system/user/search", ([FromQuery] string? realName, [FromQuery]int pageNum = 1, [FromQuery]int pageSize = 10) => Locator.Current.GetService<DetectUserService>()!.Search(realName, pageNum, pageSize));
        
        // 用户列表(按条件查询)
        webApp?.MapGet("/v1/system/user/list", (string username) => Locator.Current.GetService<DetectUserService>()!.List(x=> x.Username == username));
        
        // 新增用户
        webApp?.MapPost("/v1/system/user/add", ([FromBody] UserEntity user) => Locator.Current.GetService<DetectUserService>()!.AddData(user));

        // 修改用户
        webApp?.MapPost("/v1/system/user/update", ([FromBody] UserEntity user) => Locator.Current.GetService<DetectUserService>()!.UpdateData(user));
        
        // 根据ID删除用户
        webApp?.MapGet("/v1/system/user/delete", (long id) => Locator.Current.GetService<DetectUserService>()!.DeleteData(id));
        
        // 根据ID查询用户
        webApp?.MapGet("/v1/system/user", (long id) => Locator.Current.GetService<DetectUserService>()!.GetDataById(id));
        
        // id
        webApp?.MapGet("/v1/system/user/{id:long}", ([FromRoute] long id) => 
            Locator.Current.GetService<DetectUserService>()!.GetDataById(id));
    }
}