This commit is contained in:
Christian Arenas 2017-06-02 16:30:39 +02:00
parent f993c8c33d
commit a5aea54cc8
3 changed files with 152 additions and 12 deletions

View File

@ -5,8 +5,11 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.eShopOnContainers.Services.Marketing.API.Model; using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
using System.Collections.Generic;
[Route("api/[controller]")] [Route("api/v1/[controller]")]
//[Authorize]
public class CampaignsController : Controller public class CampaignsController : Controller
{ {
private readonly MarketingContext _context; private readonly MarketingContext _context;
@ -16,6 +19,7 @@
_context = context; _context = context;
} }
[HttpGet] [HttpGet]
public async Task<IActionResult> GetAllCampaigns() public async Task<IActionResult> GetAllCampaigns()
{ {
@ -23,7 +27,9 @@
.Include(c => c.Rules) .Include(c => c.Rules)
.ToListAsync(); .ToListAsync();
return Ok(campaignList); var campaignDtoList = CampaignModelListToDtoList(campaignList);
return Ok(campaignDtoList);
} }
[HttpGet("{id:int}")] [HttpGet("{id:int}")]
@ -31,30 +37,44 @@
{ {
var campaign = await _context.Campaigns var campaign = await _context.Campaigns
.Include(c => c.Rules) .Include(c => c.Rules)
.SingleAsync(c => c.Id == id); .SingleOrDefaultAsync(c => c.Id == id);
if (campaign is null) if (campaign is null)
{ {
return NotFound(); return NotFound();
} }
return Ok(campaign); var campaignDto = CampaignModelToDto(campaign);
return Ok(campaignDto);
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> CreateCampaign([FromBody] Campaign campaign) public async Task<IActionResult> CreateCampaign([FromBody] CampaignDTO campaign)
{ {
await _context.Campaigns.AddAsync(campaign); if (campaign is null)
{
return BadRequest();
}
var campaingToCreate = CampaignDtoToModel(campaign);
await _context.Campaigns.AddAsync(campaingToCreate);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetCampaignById), new { id = campaign.Id }, null); return CreatedAtAction(nameof(GetCampaignById), new { id = campaingToCreate.Id }, null);
} }
[HttpPut("{id:int}")] [HttpPut("{id:int}")]
public async Task<IActionResult> UpdateCampaign(int id, [FromBody]Campaign campaign) public async Task<IActionResult> UpdateCampaign(int id, [FromBody]CampaignDTO campaign)
{ {
if (id < 1 || campaign is null)
{
return BadRequest();
}
var campaignToUpdate = await _context.Campaigns.FindAsync(id); var campaignToUpdate = await _context.Campaigns.FindAsync(id);
if (campaign is null) if (campaignToUpdate is null)
{ {
return NotFound(); return NotFound();
} }
@ -71,16 +91,92 @@
[HttpDelete("{id:int}")] [HttpDelete("{id:int}")]
public async Task<IActionResult> Delete(int id) public async Task<IActionResult> Delete(int id)
{ {
var campaign = await _context.Campaigns.FindAsync(id); if (id < 1)
if (campaign is null) {
return BadRequest();
}
var campaignToDelete = await _context.Campaigns.FindAsync(id);
if (campaignToDelete is null)
{ {
return NotFound(); return NotFound();
} }
_context.Campaigns.Remove(campaign); _context.Campaigns.Remove(campaignToDelete);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return NoContent(); return NoContent();
} }
private List<CampaignDTO> CampaignModelListToDtoList(List<Campaign> campaignList)
{
var campaignDtoList = new List<CampaignDTO>();
campaignList.ForEach(campaign => campaignDtoList
.Add(CampaignModelToDto(campaign)));
return campaignDtoList;
}
private CampaignDTO CampaignModelToDto(Campaign campaign)
{
var campaignDto = new CampaignDTO
{
Description = campaign.Description,
From = campaign.From,
To = campaign.To,
Url = campaign.Url,
};
campaign.Rules.ForEach(c =>
{
switch ((RuleTypeEnum)c.RuleTypeId)
{
case RuleTypeEnum.UserLocationRule:
var userLocationRule = c as UserLocationRule;
campaignDto.Rules.Add(new RuleDTO
{
LocationId = userLocationRule.LocationId,
RuleTypeId = userLocationRule.RuleTypeId,
Description = userLocationRule.Description
});
break;
}
});
return campaignDto;
}
private Campaign CampaignDtoToModel(CampaignDTO campaignDto)
{
var campaingModel = new Campaign
{
Description = campaignDto.Description,
From = campaignDto.From,
To = campaignDto.To,
Url = campaignDto.Url
};
campaignDto.Rules.ForEach(c =>
{
switch (c.RuleType)
{
case RuleTypeEnum.UserLocationRule:
campaingModel.Rules.Add(new UserLocationRule
{
LocationId = c.LocationId.Value,
RuleTypeId = (int)c.RuleType,
Description = c.Description,
Campaign = campaingModel
});
break;
}
});
return campaingModel;
}
} }
} }

View File

@ -0,0 +1,25 @@
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
{
using System;
using System.Collections.Generic;
public class CampaignDTO
{
public int Id { get; set; }
public string Description { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
public string Url { get; set; }
public List<RuleDTO> Rules { get; set; }
public CampaignDTO()
{
Rules = new List<RuleDTO>();
}
}
}

View File

@ -0,0 +1,19 @@
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
{
public class RuleDTO
{
public int Id { get; set; }
public RuleTypeEnum RuleType => (RuleTypeEnum) RuleTypeId;
public int RuleTypeId { get; set; }
public int CampaignId { get; set; }
public int? LocationId { get; set; }
public string Description { get; set; }
}
public enum RuleTypeEnum { UserProfileRule = 1, PurchaseHistoryRule = 2, UserLocationRule = 3 }
}