using Dapper;
using MemoEngine.Models;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace MemoEngine.Repositories
{
public class MaximServiceRepository
{
// Database 개체 생성
private IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
public Maxim AddMaxim(Maxim maxim)
{
string sql = @"
Insert Into Maxims (Name, Content) Values (@Name, @Content);
Select Cast(SCOPE_IDENTITY() As Int);
";
var id = this.db.Query<int>(sql, maxim).Single();
maxim.Id = id;
return maxim;
}
public List<Maxim> GetMaxims()
{
string sql = "Select Id, Name, Content, CreatoinDate From Maxims Order By Id Asc";
return this.db.Query<Maxim>(sql).ToList();
}
public Maxim GetMaximById(int id)
{
string sql = "Select * From Maxims Where Id = @Id";
return this.db.Query<Maxim>(sql, new { Id = id }).SingleOrDefault();
}
public Maxim UpdateMaxim(Maxim model)
{
string sql = "Update Maxims Set Name = @Name, Content = @Content Where Id = @Id";
this.db.Execute(sql, model);
return model;
}
public void RemoveMaxim(int id)
{
string sql = "Delete Maxims Where Id = @Id";
this.db.Execute(sql, new { id });
}
}
}