WPF实战学习笔记13-创建注册登录接口

创建注册登录接口

添加文件

创建文件

+ MyToDo.Api

​ ./Controllers/LoginController.cs

​ ./Service/ILoginService.cs

​ ./Service/LoginService.cs

  • MyToDo.Share

    ./Dtos/UserDto.cs

LoginController.cs
using Microsoft.AspNetCore.Mvc;
using MyToDo.Api.Context;
using MyToDo.Api.Service;
using MyToDo.Share.Dtos;
using MyToDo.Share.Parameters;

namespace MyToDo.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class LoginController : ControllerBase
    {

        private readonly ILoginService service;

        public LoginController(ILoginService tService)
        {
            this.service = tService;
        }

        [HttpGet]
        public async Task LoginAsync(string Account, string PassWord) => await service.LoginAsync(Account,PassWord);

        [HttpPost]
        public async Task Resgiter([FromBody] UserDto param) => await service.Resgiter(param);
    }
}

ILoginService.cs
using MyToDo.Share.Dtos;

namespace MyToDo.Api.Service
{
    public interface ILoginService
    {
        /// 
        /// 登录
        /// 
        /// 登录名
        /// 登录密码
        /// 
        Task LoginAsync(string Account,string PassWord);

        /// 
        /// 注册
        /// 
        /// 
        Task Resgiter(UserDto user);
    }
}

LoginService.cs
using Arch.EntityFrameworkCore.UnitOfWork;
using AutoMapper;
using MyToDo.Api.Context;
using MyToDo.Share.Dtos;
using System.Diagnostics.Eventing.Reader;

namespace MyToDo.Api.Service
{
    public class LoginService : ILoginService
    {
        private readonly IUnitOfWork work;
        private readonly IMapper mapper;

        public LoginService(IUnitOfWork work, IMapper mapper)
        {
            this.work = work;
            this.mapper = mapper;
        }

        public async Task LoginAsync(string Account, string PassWord)
        {
            try
            {
                if(Account==null || PassWord==null)
                    return new ApiReponse("账号或密码为空", false);

                var model = await work.GetRepository().GetFirstOrDefaultAsync(predicate: x => (x.Account.Equals(Account)) && (x.Password.Equals(PassWord)));

                if(model == null)
                {
                    return new ApiReponse("账号或密码错误",false); 
                }

                return new ApiReponse(true,model);
            }
            catch (Exception ex)
            {

                return new ApiReponse("登录失败", false); ;
            }
        }

        public async Task Resgiter(UserDto user)
        {
            try
            {
                var model = mapper.Map(user);

                var repository = work.GetRepository();
                //.GetFirstOrDefaultAsync(predicate: x => x.Account.Equals(model.Account));

                var usermodel = await repository.GetFirstOrDefaultAsync(predicate: x => x.Account.Equals(model.Account));

                if(usermodel != null) 
                    return new ApiReponse("当前账户已存在",false);  
               
                model.CreateDate = DateTime.Now;

                await repository.InsertAsync(model);

                if(await work.SaveChangesAsync()>0)
                    return new ApiReponse(true, model);

                return new ApiReponse("注册失败,请稍后重试", false);

            }
            catch (Exception ex)
            {
                return new ApiReponse($"注册失败.{ex.Message}", false);
            }
        }
    }
}

UserDto.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyToDo.Share.Dtos
{
    public class UserDto:BaseDto
    {
		private string? userName;
		private string? passWord;
		private string? account;

		/// 
		/// 账户
		/// 
		public string? Account
		{
			get { return account; }
			set { account = value; OnPropertyChanged(); }
		}

		/// 
		/// 密码
		/// 
		public string PassWord
		{
			get { return passWord; }
			set { passWord = value; OnPropertyChanged(); }
		}

		/// 
		/// 用户名
		/// 
		public string UserName
		{
			get { return userName; }
			set { userName = value; OnPropertyChanged(); }
		}

	}
}

依赖注入

Program.cs 添加
.AddCustomRepository();
AutoMapperProfilec.s 添加
builder.Services.AddTransient();

你可能感兴趣的:(WPF实战学习笔记,wpf,学习,笔记)