tycg_carviolation_BE/Traffic.Repository/Implements/RepairRepository.cs

71 lines
2.6 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 RepairRepository : IRepairRepository
{
public IDbTransaction Transaction { get; }
public IDbConnection Connection => Transaction.Connection;
public RepairRepository(IDbTransaction transaction)
{
Transaction = transaction;
}
public IEnumerable<Repair> GetRepairs()
{
var sql = $"SELECT * FROM {nameof(Repair)}";
return Connection.Query<Repair>(sql, null, Transaction);
}
public Repair GetRepairById(int id)
{
var sql = $"SELECT * FROM {nameof(Repair)} Where {nameof(Repair.Id)} = @{nameof(Repair.Id)}";
return Connection.QueryFirstOrDefault<Repair>(sql, new { id }, Transaction);
}
public bool InsertRepair(Repair data)
{
var sql = $@"INSERT INTO {nameof(Repair)} (
{nameof(Repair.QuestionType)},
{nameof(Repair.SendTo)},
{nameof(Repair.Title)},
{nameof(Repair.Content)})
VALUES(@{nameof(Repair.QuestionType)},
@{nameof(Repair.SendTo)},
@{nameof(Repair.Title)},
@{nameof(Repair.Content)})";
var result = Connection.Execute(sql, data, Transaction);
return result == 1;
}
public bool UpdateRepair(Repair data)
{
var sql = $@"UPDATE {nameof(Repair)}
SET {nameof(Repair.QuestionType)} = @{nameof(Repair.QuestionType)},
{nameof(Repair.SendTo)} = @{nameof(Repair.SendTo)},
{nameof(Repair.Title)} = @{nameof(Repair.Title)},
{nameof(Repair.Content)} = @{nameof(Repair.Content)}
WHERE {nameof(Repair.Id)} = @{nameof(Repair.Id)}";
var result = Connection.Execute(sql, data, Transaction);
return result == 1;
}
public bool DeleteRepair(int id)
{
var sql = $@"DELETE FROM {nameof(Repair)} WHERE {nameof(Repair.Id)} = @{nameof(Repair.Id)}";
var result = Connection.Execute(sql, new { id }, Transaction);
return result == 1;
}
}
}