火车售票系统(Web服务开发)

WebAPI完整代码:
WebAPI(第一组)

客户端完整代码:
TrainTicketSystem(第一组)

一、WebAPI

本次实验中,我们将用户类与数据库相关的操作封装在了webapi里,在第二次实验的基础上,查询账号密码、登录、注册等功能将改变为通过自己设计的api间接访问数据库,而不是直接操作数据库。
基于Rest风格的webapi(WebServer项目)

(1)model层

model层即数据库实体层,也被称为entity层。
一般数据库一张表对应一个实体类,类属性同表字段一一对应。
在本项目中,model层中的Product类即对应于管理员类。里面的属性账号、密码分别于数据库中管理员表的字段一一对应。

namespace Webserver.Models
{
    public class Product
    {
        private int id;
        private int password;

        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public int Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
            }
        }

    }
}

(2)repository层

这一层的作用为访问数据库,向数据库发送sql语句,完成数据的增删改查任务。
本项目中,我们在ProductRepository.cs中访问数据库,包括查询账号密码是否正确、当前账号是否在表中已经存在、新增账号和删除管理员账户几个功能。

//查询账号密码是否正确以便于登录
        public bool CheckIsPwdCorrect(int Account, int Password)
        {
            DatabaseConnection();
            try
            {
                string sql = "select password from adm_id where Adm_ID=" + Account + ";";
                MySqlCommand command = new MySqlCommand(sql, Conn);
                MySqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    if (reader.HasRows && reader.GetString(0) == Password.ToString())
                    {
                        reader.Close();
                        return true;
                    }
                }
                reader.Close();
                Conn.Close();
                return false;
            }
            catch (Exception e)
            {
                Conn.Close();
                return false;
                throw new Exception("查询失败" + e.Message);
            }

        }

(3)controller层

controller层即控制层,功能为请求和响应控制。
controller层负责前后端交互,接受前端请求,对于请求作出相应的处理,的数据,最后返回具体的页面或数据到客户端。
在UserController.cs中,我们只关心操作结果,于是这一块调用repository中的方法时,用的主要都是GET方法来获取是否操作成功。

 public bool GetIsPwdCorrect(int id, int pwd)
        {
            return repository.CheckIsPwdCorrect(id,pwd);
        }

二、客户端调用WebAPI

我们使用用HttpClient来进行Web Api的调用。由于WebApi的调用本质上就是一次普通的发送请求与接收响应的过程,所有HttpClient其实可以作为一般意义上发送HTTP请求的工具。
如在登录界面,用户输入相应账号和密码后,按下登录按钮后调用CheckIsPwdCorrect()方法,这里将我们封装好的WebAPi中相对应的检查账户和密码是否匹配的函数GetIsPwdCorrect及其参数赋值id和pwd写入url,并通过HttpClient发起调用。
由于我们这里主要实现用户类的WebApi的封装与调用,所以我们只在用户类的数据库相关操作上改变为通过自己设计的api间接访问数据库。

(1)注册

  1. 注册时判断账号是否重复
 //checkIsAcclegal()方法
 public string CheckIsAccLegal(int Account)//判断账号是否重复
        {
            //int id = int.Parse(tbAccount.Text);
            HttpClient client = new HttpClient();
            string url = "https://localhost:44376/api/User/GetAccLegal?id=" + Account;
            string html = client.GetStringAsync(url).Result;
            return html;
        }
  1. 添加账户
//AddManager()方法
      public void AddManager()    //添加账户
        {
            string id = tbAccount.Text;
            string pwd = tbPassword.Text;
            HttpClient client = new HttpClient();
            string url = "https://localhost:44376/api/User/GetAdd?id=" + id + "&pwd=" + pwd;
            string res = client.GetStringAsync(url).Result;
        }

(2)登录

  1. 登录时检查账号密码是否匹配
//CheckIsPwdCorrect()方法
public string CheckIsPwdCorrect()//检查账号密码是否匹配
        {
            string id = AccountText.Text;
            string pwd = PasswordText.Text;
            HttpClient client = new HttpClient();
            string url = "https://localhost:44376/api/User/GetIsPwdCorrect?id=" + id + "&pwd=" + pwd;
            string html = client.GetStringAsync(url).Result;
            //MessageBox.Show(html);
            return html;
        }

(3)删除账户

  1. 删除
//DeleteManager()方法
        public string DeleteManager()    //删除账户
        {
            int id = int.Parse(AccountText.Text);
            //string pwd = PasswordText.Text;
            HttpClient client = new HttpClient();
            string url = "https://localhost:44376/api/User/GetDelUser?id=" + id;
            string html = client.GetStringAsync(url).Result;
            return html;
            //MessageBox.Show(html);
        }

运行结果截图

(1)WebAPI运行

WebAPI运行

(2)注册

注册
注册成功

(3)登录

登录

(4)删除

删除账户
删除成功

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值