该投票系统实现的功能较为简单,适合初学者借鉴。
数据库表只有三张:用户表、投票信息表和投票选项表,采用的是SQLServer数据库,后端采用ASP .NET框架(C#),前端采用Angular,UI组件采用的是NG-ZORRO。功能较简单,有用户登录与注册、查看投票列表,进行投票,发起投票,查看投票结果等功能。
第一步:在Visual Studio 2019创建一个新的项目
第二步:创建数据实体类的文件夹Models
User.cs
using System.ComponentModel.DataAnnotations.Schema;
namespace Vote_Api.Models
{
[Table("UserInfo")]
public class User
{
public string UserId { get; set; }
public string Passwd { get; set; }
public string Tel { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
}
VoteInfo.cs
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace Vote_Api.Models
{
[Table("VoteInfo")]
public class VoteInfo
{
public int ID { get; set; }
public string Vote_Title { get; set; }
public int? Vote_Sum { get; set; }
public DateTime Start_Time { get; set; }
public DateTime End_Time { get; set; }
public bool Vote_Type { get; set; }
public string UserID { get; set; }
}
}
VoteOption.cs
using System.ComponentModel.DataAnnotations.Schema;
namespace Vote_Api.Models
{
[Table("VoteOption")]
public class VoteOption
{
public int ID { get; set; }
public string Vote_Item { get; set; }
public int Vote_Num { get; set; }
public int Vote_ID { get; set; }
}
}
第三步:在appsettings.json添加配置数据库连接字符串
"ConnectionStrings": {
"sqlserverConn": "Server=地址;Database=数据库名;user id=用户名;password=密码;"
}
第四步:在Models中创建一个类继承DbContext
using Microsoft.EntityFrameworkCore;
namespace Vote_Api.Models
{
public class VoteContext :DbContext
{
public VoteContext(DbContextOptions<VoteContext> options) : base(options) { }
public DbSet<User> User_Info { get; set; }
public DbSet<VoteInfo> Vote_Info { get; set; }
public DbSet<VoteOption> Vote_Option { get; set; }
}
}
还要在Startup.cs的ConfigureService方法中添加注入
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<VoteContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("sqlserverConn"))); //"sqlserverConn"是之前在appsettings.json中配置的连接字符串
}
第五步:编写业务逻辑
IVoteInfoService.cs
using System;
using Vote_Api.Models;
namespace Vote_Api.Service.IService
{
public interface IVoteInfoService
{
int AddVote(VoteInfo vote); //发起投票
bool AddVoteOption(VoteOption voteop); //添加投票选项
string getAllVotes(); //获取所有投票列表
string getVoteById(int id); //查看某个投票详情
string getVoteOptionById(int id); //获取某个投票选项
bool doVote(int id); //进行投票
string getMyVotes(string user_id); //获取某个用户发起的投票列表
}
}
接口实现类VoteInfoService.cs
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using Vote_Api.Models;
using Vote_Api.Service.IService;
namespace Vote_Api.Service.ServiceImpl
{
public class VoteInfoService : IVoteInfoService
{
private readonly VoteContext _context;
public VoteInfoService(VoteContext context)
{
_context = context; //注入数据库上下文
}
public int AddVote(VoteInfo vote)
{
_context.Vote_Info.Add(vote);
int res = _context.SaveChanges();
if(res == 1)
{
return vote.ID; //添加投票信息成功后返回id
}
else
{
return 0;
}
}
public bool AddVoteOption(VoteOption voteop)
{
_context.Vote_Option.Add(voteop);
int res = _context.SaveChanges();
if(res == 1)
{
return true;
}
else
{
return false;
}
}
public string getAllVotes()
{
List<VoteInfo> votes = _context.Vote_Info.ToList();
_context.SaveChanges();
return JsonConvert.SerializeObject(votes); //序列化
}
public string getVoteById(int id)
{
VoteInfo vote = _context.Vote_Info.Find(id);
_context.SaveChanges();
return JsonConvert.SerializeObject(vote);
}
public string getVoteOptionById(int id)
{
string sql = "select * from VoteOption where Vote_ID = "+id;
var voteOption = _context.Vote_Option.FromSqlRaw(sql); //采用原生的Sql语句
_context.SaveChanges();
return JsonConvert.SerializeObject(voteOption);
}
public bool doVote(int id)
{
VoteOption vop = _context.Vote_Option.Find(id);
vop.Vote_Num += 1;
VoteInfo v =_context.Vote_Info.Find(vop.Vote_ID);
v.Vote_Sum += 1;
int res = _context.SaveChanges();
Console.WriteLine(res);
if (res == 2 )
{
return true;
}
else
{
return false;
}
}
public string getMyVotes(string user_id)
{
string sql = "select * from VoteInfo where UserID = '" + user_id+"'";
var myvote = _context.Vote_Info.FromSqlRaw(sql);
_context.SaveChanges();
return JsonConvert.SerializeObject(myvote);
}
}
}
第六步:同样需要在在Startup.cs中添加注入服务
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<VoteContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("sqlserverConn")));
services.AddTransient<IVoteInfoService, VoteInfoService>();//注入刚刚创建的接口和实现类
}
第七步:创建Controller,编写接口
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Vote_Api.Models;
using Vote_Api.Service.IService;
namespace Vote_Api.Controllers
{
[Route("api/vote")]
[ApiController]
public class VoteController : Controller
{
private readonly IVoteInfoService _service;
public VoteController(IVoteInfoService service)
{
_service = service;
}
[HttpGet("getAllVotes")]
public string getAllVotes()
{
return _service.getAllVotes();
}
[HttpGet("getVoteById")]
public string getVoteById(int vote_id)
{
return _service.getVoteById(vote_id);
}
[HttpGet("getVoteOptionById")]
public string getVoteOptionById(int vote_option_id)
{
return _service.getVoteOptionById(vote_option_id);
}
[HttpGet("doVote")]
public bool doVote(int vote_option_id)
{
return _service.doVote(vote_option_id);
}
[HttpGet("getMyVotes")]
public string getMyVotes(string user_id)
{
return _service.getMyVotes(user_id);
}
[HttpPost("AddVote")]
public int AddVote(VoteInfo vote)
{
return _service.AddVote(vote);
}
[HttpPost("AddVoteOption")]
public bool AddVoteOption(VoteOption voteop)
{
return _service.AddVoteOption(voteop);
}
}
}
第八步:postman测试api
到此投票模块的功能api已经简单完成,下一篇会讲登录模块的api实现。写得很简单,适合初学者。如有不足,评论区见。