using Angular.App.Data.EntityConfig;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
namespace Angular.App.Data.Repository
{
publicclass CelularRepository
{
privatereadonly DataContext _context = new DataContext();
//獲取晶片型別public CelularModel ObterPorTipoChip(string tipoChip)
{
var sql = @"select * from celulares c" + "Where c.tipochip = @tipoChip";
using (var cn = _context.Database.Connection)
{
cn.Open();
var cliente = cn.Query<CelularModel>(sql);
return cliente.FirstOrDefault();
}
}
//按品牌獲取public CelularModel ObterPorMarca(string marca)
{
var sql = @"select * from celulares c" + "Where c.marca = @marca";
using (var cn = _context.Database.Connection)
{
cn.Open();
var cliente = cn.Query<CelularModel>(sql);
return cliente.FirstOrDefault();
}
}
//由Idpublic CelularModel ObterPorId(int id)
{
var sql = @"select * from celulares c " + "Where c.id = @id";
using (var cn = _context.Database.Connection)
{
cn.Open();
var cliente = cn.Query<CelularModel>(sql, new { id = id });
return cliente.FirstOrDefault();
}
}
public IQueryable<CelularModel> ObterTodos()
{
return _context.Celular;
}
//更新publicvoidAtualizar(CelularModel celular)
{
_context.Entry(celular).State = EntityState.Modified;
_context.SaveChanges();
}
//新增publicvoidIncluir(CelularModel celular)
{
_context.Celular.Add(celular);
_context.SaveChanges();
}
//刪除publicvoidExcluir(int id)
{
_context.Celular.Remove(_context.Celular.Find(id));
_context.SaveChanges();
}
publicvoidDispose()
{
_context.Dispose();
GC.SuppressFinalize(this);
}
}
}
ClienteService.cs
using Angular.App.Data.EntityConfig;
using Angular.App.Data.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Angular.App.Application
{
publicclass ClienteService
{
private CelularRepository _repository = new CelularRepository();
public CelularModel ObterPorTipoChip(string tipoChip)
{
return _repository.ObterPorTipoChip(tipoChip);
}
public CelularModel ObterPorId(int id)
{
return _repository.ObterPorId(id);
}
public IQueryable<CelularModel> ObterTodos()
{
return _repository.ObterTodos();
}
publicvoidAtualizar(CelularModel celular)
{
_repository.Atualizar(celular);
}
publicvoidIncluir(CelularModel celular)
{
_repository.Incluir(celular);
}
publicvoidExcluir(int id)
{
_repository.Excluir(id);
}
publicvoidDispose()
{
_repository.Dispose();
GC.SuppressFinalize(this);
}
}
}
DataContext.cs
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Angular.App.Data.EntityConfig;
namespace Angular.App.Data
{
publicclass DataContext : DbContext
{
publicDataContext()
: base("DefaultConnection")
{
}
public DbSet<CelularModel> Celular { get; set; }
protectedoverridevoidOnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Configurations.Add(new CelularConfiguration());
}
}
}
CelularConfiguration.cs
using System.Data.Entity.ModelConfiguration;
namespace Angular.App.Data.EntityConfig
{
public class CelularConfiguration: EntityTypeConfiguration<CelularModel>
{
public CelularConfiguration()
{
HasKey(c => c.Id);
Property(c => c.Marca).IsRequired().HasMaxLength(50);
Property(c => c.Modelo).IsRequired().HasMaxLength(50);
Property(c => c.Cor).IsRequired().HasMaxLength(30);
Property(c => c.TipoChip).IsRequired().HasMaxLength(30);
Property(c => c.MemoriaInterna).IsRequired().HasMaxLength(3);
ToTable("Celulares");
}
}
}
CelularController.cs
using Angular.App.Application;
using Angular.App.Data.EntityConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Angular.App.UI.Controllers
{
[RoutePrefix("api/v1/public")]
publicclass CelularController : ApiController
{
private ClienteService _db = new ClienteService();
[HttpGet]
[Route("celulares")]
//獲取所有手機public IQueryable<CelularModel> ObterTodosCelulares()
{
return _db.ObterTodos();
}
[HttpGet]
[Route("celular/{id:int}")]
//通過ID獲取手機public HttpResponseMessage ObterCelularPorId(int id)
{
if (id <= 0)
return Request.CreateResponse(HttpStatusCode.BadRequest);
return Request.CreateResponse(HttpStatusCode.OK, _db.ObterPorId(id));
}
[HttpPut]
[Route("putCelular")]
//更新手機public HttpResponseMessage AtualizarCelular(CelularModel celular)
{
if (celular == null)
return Request.CreateResponse(HttpStatusCode.BadRequest);
_db.Atualizar(celular);
return Request.CreateResponse(HttpStatusCode.OK);
}
[HttpPost]
[Route("postCelular")]
//新增手機public HttpResponseMessage AdicionarCelular(CelularModel celular)
{
if (celular == null)
return Request.CreateResponse(HttpStatusCode.BadRequest);
_db.Incluir(celular);
return Request.CreateResponse(HttpStatusCode.OK);
}
[HttpDelete]
[Route("deleteCelular/{id:int}")]
//刪除手機public HttpResponseMessage RemoverCelular(int id)
{
if (id <= 0)
return Request.CreateResponse(HttpStatusCode.BadRequest);
_db.Excluir(id);
return Request.CreateResponse(HttpStatusCode.OK);
}
}
}
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Angular.App.UI.Controllers
{
publicclass HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}