123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using Dapper;
- using GCAS.Model;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Threading.Tasks;
- namespace GCAS.Code
- {
- public class EntranceRepository
- {
- public async Task<int> Insert(EntranceModel entity)
- {
- string exitSql = "Select count(1) from entrance where name=@Name";
- string insertSql = "INSERT into entrance (name,time) VALUES (@Name,NOW())";
- using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
- {
- var count = await conn.QueryFirstAsync<int>(exitSql, new { entity.Name });
- if (count > 0)
- {
- return -1;
- }
- return await conn.ExecuteAsync(insertSql, entity);
- }
- }
- public async Task<int> Update(EntranceModel entity)
- {
- string exitSql = "Select count(1) from entrance where name=@Name and id<>@Id";
- string insertSql = "Update entrance set name=@Name where id=@Id";
- using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
- {
- var count = await conn.QueryFirstAsync<int>(exitSql, entity);
- if (count > 0)
- {
- return -1;
- }
- return await conn.ExecuteAsync(insertSql, entity);
- }
- }
- public async Task<int> Delete(int id)
- {
- string updateSql = "Delete from entrance WHERE id=@Id";
- using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
- {
- return await conn.ExecuteAsync(updateSql, new { Id = id });
- }
- }
- public List<EntranceModel> GetList()
- {
- string selectSql = "SELECT id,name,time from entrance ORDER BY id ASC";
- using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
- {
- return conn.Query<EntranceModel>(selectSql).ToList();
- }
- }
- public async Task<EntranceModel> Get(int id)
- {
- string selectSql = "SELECT id,name,time FROM entrance WHERE id=@Id";
- using (IDbConnection conn = DataBaseConfig.GetSqlConnection())
- {
- return await Task.Run(() => conn.QueryFirstOrDefault<EntranceModel>(selectSql, new { Id = id }));
- }
- }
- }
- }
|