Skip to content

Commit a5aea54

Browse files
committed
Add dtos
1 parent f993c8c commit a5aea54

3 files changed

Lines changed: 152 additions & 12 deletions

File tree

src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs

Lines changed: 108 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
using System.Threading.Tasks;
66
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
77
using Microsoft.EntityFrameworkCore;
8+
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
9+
using System.Collections.Generic;
810

9-
[Route("api/[controller]")]
11+
[Route("api/v1/[controller]")]
12+
//[Authorize]
1013
public class CampaignsController : Controller
1114
{
1215
private readonly MarketingContext _context;
@@ -16,45 +19,62 @@ public CampaignsController(MarketingContext context)
1619
_context = context;
1720
}
1821

22+
1923
[HttpGet]
2024
public async Task<IActionResult> GetAllCampaigns()
2125
{
2226
var campaignList = await _context.Campaigns
2327
.Include(c => c.Rules)
2428
.ToListAsync();
2529

26-
return Ok(campaignList);
30+
var campaignDtoList = CampaignModelListToDtoList(campaignList);
31+
32+
return Ok(campaignDtoList);
2733
}
2834

2935
[HttpGet("{id:int}")]
3036
public async Task<IActionResult> GetCampaignById(int id)
3137
{
3238
var campaign = await _context.Campaigns
3339
.Include(c => c.Rules)
34-
.SingleAsync(c => c.Id == id);
40+
.SingleOrDefaultAsync(c => c.Id == id);
3541

3642
if (campaign is null)
3743
{
3844
return NotFound();
3945
}
4046

41-
return Ok(campaign);
47+
var campaignDto = CampaignModelToDto(campaign);
48+
49+
return Ok(campaignDto);
4250
}
4351

4452
[HttpPost]
45-
public async Task<IActionResult> CreateCampaign([FromBody] Campaign campaign)
53+
public async Task<IActionResult> CreateCampaign([FromBody] CampaignDTO campaign)
4654
{
47-
await _context.Campaigns.AddAsync(campaign);
55+
if (campaign is null)
56+
{
57+
return BadRequest();
58+
}
59+
60+
var campaingToCreate = CampaignDtoToModel(campaign);
61+
62+
await _context.Campaigns.AddAsync(campaingToCreate);
4863
await _context.SaveChangesAsync();
4964

50-
return CreatedAtAction(nameof(GetCampaignById), new { id = campaign.Id }, null);
65+
return CreatedAtAction(nameof(GetCampaignById), new { id = campaingToCreate.Id }, null);
5166
}
5267

5368
[HttpPut("{id:int}")]
54-
public async Task<IActionResult> UpdateCampaign(int id, [FromBody]Campaign campaign)
69+
public async Task<IActionResult> UpdateCampaign(int id, [FromBody]CampaignDTO campaign)
5570
{
71+
if (id < 1 || campaign is null)
72+
{
73+
return BadRequest();
74+
}
75+
5676
var campaignToUpdate = await _context.Campaigns.FindAsync(id);
57-
if (campaign is null)
77+
if (campaignToUpdate is null)
5878
{
5979
return NotFound();
6080
}
@@ -71,16 +91,92 @@ public async Task<IActionResult> UpdateCampaign(int id, [FromBody]Campaign campa
7191
[HttpDelete("{id:int}")]
7292
public async Task<IActionResult> Delete(int id)
7393
{
74-
var campaign = await _context.Campaigns.FindAsync(id);
75-
if (campaign is null)
94+
if (id < 1)
95+
{
96+
return BadRequest();
97+
}
98+
99+
var campaignToDelete = await _context.Campaigns.FindAsync(id);
100+
if (campaignToDelete is null)
76101
{
77102
return NotFound();
78103
}
79104

80-
_context.Campaigns.Remove(campaign);
105+
_context.Campaigns.Remove(campaignToDelete);
81106
await _context.SaveChangesAsync();
82107

83108
return NoContent();
84109
}
110+
111+
112+
113+
private List<CampaignDTO> CampaignModelListToDtoList(List<Campaign> campaignList)
114+
{
115+
var campaignDtoList = new List<CampaignDTO>();
116+
117+
campaignList.ForEach(campaign => campaignDtoList
118+
.Add(CampaignModelToDto(campaign)));
119+
120+
return campaignDtoList;
121+
}
122+
123+
private CampaignDTO CampaignModelToDto(Campaign campaign)
124+
{
125+
var campaignDto = new CampaignDTO
126+
{
127+
Description = campaign.Description,
128+
From = campaign.From,
129+
To = campaign.To,
130+
Url = campaign.Url,
131+
};
132+
133+
campaign.Rules.ForEach(c =>
134+
{
135+
switch ((RuleTypeEnum)c.RuleTypeId)
136+
{
137+
case RuleTypeEnum.UserLocationRule:
138+
var userLocationRule = c as UserLocationRule;
139+
campaignDto.Rules.Add(new RuleDTO
140+
{
141+
LocationId = userLocationRule.LocationId,
142+
RuleTypeId = userLocationRule.RuleTypeId,
143+
Description = userLocationRule.Description
144+
});
145+
break;
146+
}
147+
});
148+
149+
return campaignDto;
150+
}
151+
152+
private Campaign CampaignDtoToModel(CampaignDTO campaignDto)
153+
{
154+
var campaingModel = new Campaign
155+
{
156+
Description = campaignDto.Description,
157+
From = campaignDto.From,
158+
To = campaignDto.To,
159+
Url = campaignDto.Url
160+
};
161+
162+
campaignDto.Rules.ForEach(c =>
163+
{
164+
switch (c.RuleType)
165+
{
166+
case RuleTypeEnum.UserLocationRule:
167+
campaingModel.Rules.Add(new UserLocationRule
168+
{
169+
LocationId = c.LocationId.Value,
170+
RuleTypeId = (int)c.RuleType,
171+
Description = c.Description,
172+
Campaign = campaingModel
173+
});
174+
break;
175+
}
176+
});
177+
178+
return campaingModel;
179+
}
180+
85181
}
86182
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
6+
public class CampaignDTO
7+
{
8+
public int Id { get; set; }
9+
10+
public string Description { get; set; }
11+
12+
public DateTime From { get; set; }
13+
14+
public DateTime To { get; set; }
15+
16+
public string Url { get; set; }
17+
18+
public List<RuleDTO> Rules { get; set; }
19+
20+
public CampaignDTO()
21+
{
22+
Rules = new List<RuleDTO>();
23+
}
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
2+
{
3+
public class RuleDTO
4+
{
5+
public int Id { get; set; }
6+
7+
public RuleTypeEnum RuleType => (RuleTypeEnum) RuleTypeId;
8+
9+
public int RuleTypeId { get; set; }
10+
11+
public int CampaignId { get; set; }
12+
13+
public int? LocationId { get; set; }
14+
15+
public string Description { get; set; }
16+
}
17+
18+
public enum RuleTypeEnum { UserProfileRule = 1, PurchaseHistoryRule = 2, UserLocationRule = 3 }
19+
}

0 commit comments

Comments
 (0)