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