tycg_carviolation_BE/Traffic.Repository/Implements/PoliceStationRepository.cs

65 lines
2.4 KiB
C#
Raw Permalink Normal View History

using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traffic.Data.Models;
using Traffic.Repository.Interfaces;
namespace Traffic.Repository.Implements
{
public class PoliceStationRepository : IPoliceStationRepository
{
public IDbTransaction Transaction { get; }
public IDbConnection Connection => Transaction.Connection;
public PoliceStationRepository(IDbTransaction transaction)
{
Transaction = transaction;
}
public IEnumerable<PoliceStation> GetPoliceStations()
{
var sql = $"SELECT * FROM {nameof(PoliceStation)}";
return Connection.Query<PoliceStation>(sql, null, Transaction);
}
public PoliceStation GetPoliceStationById(int id)
{
var sql = $"SELECT * FROM {nameof(PoliceStation)} Where {nameof(PoliceStation.Id)} = @{nameof(PoliceStation.Id)}";
return Connection.QueryFirstOrDefault<PoliceStation>(sql, new { id }, Transaction);
}
public bool InsertPoliceStation(PoliceStation station)
{
var sql = $@"INSERT INTO {nameof(PoliceStation)} (
{nameof(PoliceStation.Area)},
{nameof(PoliceStation.StationName)})
VALUES(@{nameof(PoliceStation.Area)},
@{nameof(PoliceStation.StationName)})";
var result = Connection.Execute(sql, station, Transaction);
return result == 1;
}
public bool UpdatePoliceStation(PoliceStation station)
{
var sql = $@"UPDATE {nameof(PoliceStation)}
SET {nameof(PoliceStation.Area)} = @{nameof(PoliceStation.Area)},
{nameof(PoliceStation.StationName)} = @{nameof(PoliceStation.StationName)}
WHERE {nameof(PoliceStation.Id)} = @{nameof(PoliceStation.Id)}";
var result = Connection.Execute(sql, station, Transaction);
return result == 1;
}
public bool DeletePoliceStation(int id)
{
var sql = $@"DELETE FROM {nameof(PoliceStation)} WHERE {nameof(PoliceStation.Id)} = @{nameof(PoliceStation.Id)}";
var result = Connection.Execute(sql, new { id }, Transaction);
return result == 1;
}
}
}