Add dtos
This commit is contained in:
parent
f993c8c33d
commit
a5aea54cc8
@ -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<IActionResult> 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<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();
|
||||
|
||||
return CreatedAtAction(nameof(GetCampaignById), new { id = campaign.Id }, null);
|
||||
return CreatedAtAction(nameof(GetCampaignById), new { id = campaingToCreate.Id }, null);
|
||||
}
|
||||
|
||||
[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);
|
||||
if (campaign is null)
|
||||
if (campaignToUpdate is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
@ -71,16 +91,92 @@
|
||||
[HttpDelete("{id:int}")]
|
||||
public async Task<IActionResult> 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<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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
25
src/Services/Marketing/Marketing.API/Dto/CampaignDTO.cs
Normal file
25
src/Services/Marketing/Marketing.API/Dto/CampaignDTO.cs
Normal 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>();
|
||||
}
|
||||
}
|
||||
}
|
19
src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs
Normal file
19
src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs
Normal 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 }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user