using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Http.Description;
using System.Net.Mail;
using CarCheapApi.Models;
namespace CarCheapApi.Controllers
{
public class CarCheapController : ApiController
{
private CarCheapDBEntity db = new CarCheapDBEntity();
// GET: api/KarCheap
public IQueryable<Visitors> GetVisitors()
{
return db.Visitors;
}
// GET: api/KarCheap/5
[ResponseType(typeof(Visitors))]
public IHttpActionResult GetVisitors(int id)
{
Visitors visitors = db.Visitors.Find(id);
if (visitors == null)
{
return NotFound();
}
return Ok(visitors);
}
// PUT: api/KarCheap/5
[ResponseType(typeof(void))]
public IHttpActionResult PutVisitors(int id, Visitors visitors)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != visitors.ID)
{
return BadRequest();
}
db.Entry(visitors).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!VisitorsExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
[Route("api/KarCheap/InsertVisitor")]
[HttpPost]
public Visitors InsertVisitor(Visitors _visitor)
{
using (CarCheapDBEntity entities = new CarCheapDBEntity())
{
entities.Visitors.Add(_visitor);
entities.SaveChanges();
}
return _visitor;
}
[Route("api/KarCheap/InsertBuyerMessage")]
[HttpPost]
public BuyerMessage InsertBuyerMessage(BuyerMessage _buyer)
{
using (CarCheapDBEntity entities = new CarCheapDBEntity())
{
entities.BuyerMessage.Add(_buyer);
entities.SaveChanges();
}
return _buyer;
}
[Route("api/KarCheap/InsertContactUs")]
[HttpPost]
public ContactUs InsertContactUs(ContactUs _contact)
{
using (CarCheapDBEntity entities = new CarCheapDBEntity())
{
entities.ContactUs.Add(_contact);
entities.SaveChanges();
EmailContact(_contact.VisitorEmail, _contact.VisitorName, _contact.VisitorMessage);
}
return _contact;
}
//[Route("api/KarCheap/totalViews/{category}/{itemId}")]
//[HttpPost]
//public string totalViews(string category, string itemId)
//{
// int itemID = Convert.ToInt32(itemId);
// // var query = (from cy in db.AutoUsed where cy.ID.Equals(itemID) select cy);
// int viewCount = (from a in db.Visitors
// where a.categoryType.Equals(category) && a.itemId==itemID
// select a.itemId).Count();
// if (String.IsNullOrEmpty(viewCount.ToString()))
// {
// viewCount = 0;
// }
// return viewCount.ToString();
//}
[Route("api/KarCheap/SaveLocation")]
[HttpPost]
public Visitors SaveLocation(Visitors _visitor)
{
using (CarCheapDBEntity entities = new CarCheapDBEntity())
{
entities.Visitors.Add(_visitor);
entities.SaveChanges();
}
return _visitor;
}
// DELETE: api/KarCheap/5
[ResponseType(typeof(Visitors))]
public IHttpActionResult DeleteVisitors(int id)
{
Visitors visitors = db.Visitors.Find(id);
if (visitors == null)
{
return NotFound();
}
db.Visitors.Remove(visitors);
db.SaveChanges();
return Ok(visitors);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool VisitorsExists(int id)
{
return db.Visitors.Count(e => e.ID == id) > 0;
}
}
}