From a5aea54cc8e58fb13fb193f08529d7a4d2a92764 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Fri, 2 Jun 2017 16:30:39 +0200 Subject: [PATCH] Add dtos --- .../Controllers/CampaignsController.cs | 120 ++++++++++++++++-- .../Marketing.API/Dto/CampaignDTO.cs | 25 ++++ .../Marketing/Marketing.API/Dto/RuleDTO.cs | 19 +++ 3 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 src/Services/Marketing/Marketing.API/Dto/CampaignDTO.cs create mode 100644 src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs diff --git a/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs b/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs index b029bfa51..6259b69ca 100644 --- a/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs +++ b/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs @@ -5,8 +5,11 @@ using System.Threading.Tasks; using Microsoft.eShopOnContainers.Services.Marketing.API.Model; 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 { private readonly MarketingContext _context; @@ -16,6 +19,7 @@ _context = context; } + [HttpGet] public async Task GetAllCampaigns() { @@ -23,7 +27,9 @@ .Include(c => c.Rules) .ToListAsync(); - return Ok(campaignList); + var campaignDtoList = CampaignModelListToDtoList(campaignList); + + return Ok(campaignDtoList); } [HttpGet("{id:int}")] @@ -31,30 +37,44 @@ { var campaign = await _context.Campaigns .Include(c => c.Rules) - .SingleAsync(c => c.Id == id); + .SingleOrDefaultAsync(c => c.Id == id); if (campaign is null) { return NotFound(); } - return Ok(campaign); + var campaignDto = CampaignModelToDto(campaign); + + return Ok(campaignDto); } [HttpPost] - public async Task CreateCampaign([FromBody] Campaign campaign) + public async Task 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(); - return CreatedAtAction(nameof(GetCampaignById), new { id = campaign.Id }, null); + return CreatedAtAction(nameof(GetCampaignById), new { id = campaingToCreate.Id }, null); } [HttpPut("{id:int}")] - public async Task UpdateCampaign(int id, [FromBody]Campaign campaign) + public async Task UpdateCampaign(int id, [FromBody]CampaignDTO campaign) { + if (id < 1 || campaign is null) + { + return BadRequest(); + } + var campaignToUpdate = await _context.Campaigns.FindAsync(id); - if (campaign is null) + if (campaignToUpdate is null) { return NotFound(); } @@ -71,16 +91,92 @@ [HttpDelete("{id:int}")] public async Task Delete(int id) { - var campaign = await _context.Campaigns.FindAsync(id); - if (campaign is null) + if (id < 1) + { + return BadRequest(); + } + + var campaignToDelete = await _context.Campaigns.FindAsync(id); + if (campaignToDelete is null) { return NotFound(); } - _context.Campaigns.Remove(campaign); + _context.Campaigns.Remove(campaignToDelete); await _context.SaveChangesAsync(); return NoContent(); } + + + + private List CampaignModelListToDtoList(List campaignList) + { + var campaignDtoList = new List(); + + 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; + } + } } \ No newline at end of file diff --git a/src/Services/Marketing/Marketing.API/Dto/CampaignDTO.cs b/src/Services/Marketing/Marketing.API/Dto/CampaignDTO.cs new file mode 100644 index 000000000..b5676db63 --- /dev/null +++ b/src/Services/Marketing/Marketing.API/Dto/CampaignDTO.cs @@ -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 Rules { get; set; } + + public CampaignDTO() + { + Rules = new List(); + } + } +} \ No newline at end of file diff --git a/src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs b/src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs new file mode 100644 index 000000000..6a1d9aef6 --- /dev/null +++ b/src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs @@ -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 } +} \ No newline at end of file