.Net6 web API (注册和登录---数据库)

前沿

终于实现了登录 注册 数据库操作了 

兴奋的一批 呜呜 自学 我是有天赋的。不接受任何反驳,

我无敌 你随意

看看效果

代码 

 

 

定义DTo

就是类似 一种请求和返回的数据结构体

 

 

 

service 抽象类

 

service 实现具体的抽象类

using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using yanfanFlower.Common;
using yanfanFlower.Model.Entitys;
using Mapster;
using yanfanFlower.Service.User.Dto;
using yanfanFlower.Model.Enum;

namespace yanfanFlower.Service
{
    public class UserService : IUserService
    {

        //private readonly IMapper _mapper;
        //public FlowerService(IMapper mapper)
        //{
        //    _mapper = mapper;
        //}


        public UserRes GetUsers(UserReq req)
        {
            var user = DbContext.db.Queryable<Users>().First(p => p.UserName == req.UserName && p.Password == req.Password);
            if (user != null)
            {
                return user.Adapt<UserRes>();
            }
            return new UserRes();
        }

        public UserRes RegisterUser(RegisterReq req, ref string msg)
        {
            var user = DbContext.db.Queryable<Users>().First(p => p.UserName == req.UserName);
            if (user != null)
            {
                msg = "账号已存在";
                return user.Adapt<UserRes>();
            }
            else
            {
                try
                {
                    Users users = new Users();
                    users.Password = req.Password;
                    users.NickName = req.NickName;
                    users.UserName = req.UserName;
                    users.CreateTime = DateTime.Now;
                    users.UserType = (int)EnumUserType.普通用户;
                    bool res = DbContext.db.Insertable(users).ExecuteCommand() > 0;
                    if (res)
                    {
                        user = DbContext.db.Queryable<Users>().First(p => p.UserName == req.UserName && p.Password == req.Password);
                        //&& p.Password == req.Password
                        return user.Adapt<UserRes>();
                    }
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                }
            }
            return new UserRes();
        }

    }
}

然后在控制器内写 控制器

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics.Eventing.Reader;
using yanfanFlower.Common;
using yanfanFlower.Model;
using yanfanFlower.Model.Entitys;
using yanfanFlower.Service;
using yanfanFlower.Service.User.Dto;

namespace yanfanNet6WebAPi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        public IUserService _userService;

        public LoginController(IUserService userService)
        {
            _userService = userService;
        }
        /// <summary>
        /// 验证码获取
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetValidateCodeImage(string t)
        {
            var validateCodeString = Tools.CreateValidateString();
            // 将验证码存在缓存中
            MemoryHelper.SetMemory(t, validateCodeString , 5);
            // 将图片返回的二进制
            byte[] buffer = Tools.CreateValidateCodeBuffer(validateCodeString);
            return File(buffer, @"images/jpeg");
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult Check(UserReq req)
        {

            ApiResult apiResult = new ApiResult() { IsSuccess = false };
            if (string.IsNullOrEmpty(req.UserName) || string.IsNullOrEmpty(req.Password))
            {
                apiResult.Msg = "参数不能为空!";
            }
            else
            {

                UserRes user = _userService.GetUsers(req);
                if (string.IsNullOrEmpty(user.UserName))
                {
                    apiResult.Msg = "账号不存在,用户名错误或密码错误";
                }
                else
                {
                    apiResult.IsSuccess = true;
                    apiResult.Result = user.NickName;

                }
            }
            return apiResult;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult Register(RegisterReq req)
        {

            ApiResult apiResult = new ApiResult() { IsSuccess = false };
            if (string.IsNullOrEmpty(req.UserName) || string.IsNullOrEmpty(req.NickName) || string.IsNullOrEmpty(req.Password))
            {
                apiResult.Msg = "参数不能为空!";
            }
            else 
            { 
                string msg = string .Empty;
                var res = _userService.RegisterUser(req, ref msg);
                if (!string.IsNullOrEmpty(msg))
                {
                    apiResult.Msg = msg;
                }
                else
                {
                    apiResult.IsSuccess = true;
                    apiResult.Result = req.NickName;
                  
                }
            }
            return apiResult;
        }
    }
}

 

 

 推荐个 mapster 这个库 比 autoMap 好用太多了

 

哈哈哈 

 

 

然后在program.cs 注册IOC 这个方法 服务

 然后在启动

 

注册和登录

你会看数据库里面

 

我注册的用户了 

哈哈哈哈  下面就是深度理解net 方法了

和操作数据库了OK 

正是入门 NET开发。哈哈哈哈 也不过如此么

 

加油啊 少年

posted @ 2024-05-23 16:35  -鹿-  阅读(70)  评论(0编辑  收藏  举报