Created
September 25, 2012 22:52
-
-
Save gabrielgreen/3784952 to your computer and use it in GitHub Desktop.
ApiController vs Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Web.Http; | |
using DirectAgents.Domain.Concrete; | |
using EomToolWeb.Models; | |
namespace EomToolWeb.Controllers | |
{ | |
public class CampaignsApiController : ApiController | |
{ | |
private EFDbContext db = new EFDbContext(); | |
public IQueryable<CampaignViewModel> Get() | |
{ | |
var query = db.Campaigns | |
.AsEnumerable() | |
.Select(c => new CampaignViewModel(c)); | |
return query.AsQueryable(); | |
} | |
public IQueryable<CampaignViewModel> Get(string vertical, string traffictype, string search, string country, int? pid) | |
{ | |
var campaigns = db.Campaigns.AsQueryable(); | |
if (!string.IsNullOrWhiteSpace(vertical)) | |
{ | |
var cr = db.Verticals.Where(v => v.Name == vertical).FirstOrDefault(); | |
if (cr != null) | |
campaigns = campaigns.Where(c => c.Vertical.Name == vertical); | |
} | |
if (!string.IsNullOrWhiteSpace(traffictype)) | |
{ | |
var tt = db.TrafficTypes.Where(t => t.Name == traffictype).FirstOrDefault(); | |
if (tt != null) | |
campaigns = campaigns.Where(c => c.TrafficTypes.Select(t => t.Name).Contains(traffictype)); | |
} | |
if (!string.IsNullOrWhiteSpace(search)) | |
{ | |
campaigns = campaigns.Where(c => c.Name.Contains(search) || c.Description.Contains(search)); | |
} | |
if (!string.IsNullOrWhiteSpace(country)) | |
{ | |
campaigns = campaigns.Where(camp => camp.Countries.Select(c => c.CountryCode).Contains(country)); | |
} | |
if (pid != null) | |
{ | |
campaigns = campaigns.Where(c => c.Pid == pid.Value); | |
} | |
var query = campaigns | |
.AsEnumerable() | |
.Select(c => new CampaignViewModel(c)) | |
.AsQueryable(); | |
return query; | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
db.Dispose(); | |
base.Dispose(disposing); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.Mvc; | |
using DirectAgents.Domain.Abstract; | |
using DirectAgents.Domain.DTO; | |
using DirectAgents.Domain.Entities; | |
using EomToolWeb.Models; | |
namespace EomToolWeb.Controllers | |
{ | |
public class CampaignsController : Controller | |
{ | |
private ICampaignRepository campaignRepository; | |
public CampaignsController(ICampaignRepository campaignRepository) | |
{ | |
this.campaignRepository = campaignRepository; | |
} | |
public ActionResult List(string searchstring, string pid, string country, string vertical, string traffictype) | |
{ | |
var viewModel = new CampaignsListViewModel(); | |
var campaigns = campaignRepository.Campaigns; | |
if (!string.IsNullOrWhiteSpace(searchstring)) | |
{ | |
viewModel.SearchString = searchstring; | |
campaigns = campaigns.Where(c => c.Name.Contains(searchstring)); | |
} | |
int pidInt; | |
if (Int32.TryParse(pid, out pidInt)) | |
{ | |
viewModel.Pid = pidInt; | |
campaigns = campaigns.Where(c => c.Pid == pidInt); | |
} | |
if (!string.IsNullOrWhiteSpace(country)) | |
{ | |
viewModel.Country = campaignRepository.Countries.Where(c => c.CountryCode == country).FirstOrDefault(); | |
if (viewModel.Country != null) | |
campaigns = campaigns.Where(camp => camp.Countries.Select(c => c.CountryCode).Contains(country)); | |
} | |
if (!string.IsNullOrWhiteSpace(vertical)) | |
{ | |
viewModel.Vertical = campaignRepository.Verticals.Where(v => v.Name == vertical).FirstOrDefault(); | |
if (viewModel.Vertical != null) | |
campaigns = campaigns.Where(c => c.Vertical.Name == vertical); | |
} | |
if (!string.IsNullOrWhiteSpace(traffictype)) | |
{ | |
viewModel.TrafficType = campaignRepository.TrafficTypes.Where(t => t.Name == traffictype).FirstOrDefault(); | |
if (viewModel.TrafficType != null) | |
campaigns = campaigns.Where(c => c.TrafficTypes.Select(t => t.Name).Contains(traffictype)); | |
} | |
viewModel.Campaigns = campaigns.AsEnumerable(); | |
return View(viewModel); | |
} | |
public ActionResult List2(string search, string pid, string country, string vertical, string traffictype) | |
{ | |
var viewModel = new CampaignsListViewModel(); | |
if (!string.IsNullOrWhiteSpace(search)) | |
{ | |
viewModel.SearchString = search; | |
} | |
int pidInt; | |
if (Int32.TryParse(pid, out pidInt)) | |
{ | |
viewModel.Pid = pidInt; | |
} | |
if (!string.IsNullOrWhiteSpace(country)) | |
{ | |
viewModel.Country = campaignRepository.Countries.Where(c => c.CountryCode == country).FirstOrDefault(); | |
} | |
if (!string.IsNullOrWhiteSpace(vertical)) | |
{ | |
viewModel.Vertical = campaignRepository.Verticals.Where(v => v.Name == vertical).FirstOrDefault(); | |
} | |
if (!string.IsNullOrWhiteSpace(traffictype)) | |
{ | |
viewModel.TrafficType = campaignRepository.TrafficTypes.Where(t => t.Name == traffictype).FirstOrDefault(); | |
} | |
return View(viewModel); | |
} | |
public ActionResult ListByCountry() | |
{ | |
var countryCodes = campaignRepository.AllCountryCodes; | |
ViewBag.CountryCodes = countryCodes.ToList(); | |
var countries = campaignRepository.Countries.OrderByDescending(c => c.Campaigns.Count()); | |
return View(countries); | |
} | |
public ActionResult Search() | |
{ | |
var countryCodes = campaignRepository.AllCountryCodes; | |
return View(countryCodes); | |
} | |
public ActionResult ShowCountries() | |
{ | |
var countryCodes = campaignRepository.AllCountryCodes; | |
return View(countryCodes); | |
} | |
public ActionResult Show(int pid) | |
{ | |
var campaign = campaignRepository.Campaigns.Where(c => c.Pid == pid).FirstOrDefault(); | |
if (campaign == null) | |
{ | |
return Content("campaign not found"); | |
} | |
return PartialView(new CampaignViewModel(campaign)); | |
} | |
[HttpGet] | |
public ActionResult Edit(int pid) | |
{ | |
var campaign = campaignRepository.Campaigns.Where(c => c.Pid == pid).FirstOrDefault(); | |
if (campaign == null) | |
{ | |
return Content("campaign not found"); | |
} | |
return PartialView(campaign); | |
} | |
[HttpPost] | |
public ActionResult Edit(Campaign campaign) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var existingCampaign = campaignRepository.FindById(campaign.Pid); | |
if (existingCampaign != null) | |
{ | |
TryUpdateModel(existingCampaign); | |
campaignRepository.SaveChanges(); | |
} | |
// else... set a ModelState error? | |
} | |
if (Request.IsAjaxRequest()) | |
return Json(new { IsValid = ModelState.IsValid }); | |
else | |
return View(campaign); | |
} | |
[HttpPost] | |
public ActionResult Edit2(Campaign campaign) | |
{ | |
var c = campaignRepository.FindById(campaign.Pid); | |
if (c != null) | |
{ | |
c.Name = campaign.Name; | |
c.Description = campaign.Description; | |
c.PayableAction = campaign.PayableAction; | |
c.Link = campaign.Link; | |
c.Cost = campaign.Cost; | |
c.Revenue = campaign.Revenue; | |
c.ImportantDetails = campaign.ImportantDetails; | |
c.BannedNetworks = campaign.BannedNetworks; | |
c.CampaignCap = campaign.CampaignCap; | |
c.ScrubPolicy = campaign.ScrubPolicy; | |
c.EomNotes = campaign.EomNotes; | |
campaignRepository.SaveChanges(); | |
} | |
return null; | |
} | |
public ActionResult Top(TopCampaignsBy by, string traffictype) | |
{ | |
TrafficType trafficTypeEntity = null; | |
if (traffictype != null) | |
trafficTypeEntity = campaignRepository.TrafficTypes.SingleOrDefault(t => t.Name == traffictype); | |
IEnumerable<CampaignSummary> campaignSummaries; | |
if (trafficTypeEntity == null) // no matching traffic type | |
campaignSummaries = campaignRepository.TopCampaigns(20, by, null); | |
else | |
campaignSummaries = campaignRepository.TopCampaigns(20, by, traffictype); | |
var model = new TopViewModel { CampaignSummaries = campaignSummaries, By = by, TrafficType = trafficTypeEntity }; | |
return View(model); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment