Skip to content

Commit ce56a42

Browse files
committed
Redesign marketing campaign api in two different controllers (campaigns/locations)
1 parent 56c07aa commit ce56a42

9 files changed

Lines changed: 189 additions & 90 deletions

File tree

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

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using Microsoft.AspNetCore.Authorization;
1111

1212
[Route("api/v1/[controller]")]
13-
//[Authorize]
13+
[Authorize]
1414
public class CampaignsController : Controller
1515
{
1616
private readonly MarketingContext _context;
@@ -24,9 +24,13 @@ public CampaignsController(MarketingContext context)
2424
public async Task<IActionResult> GetAllCampaigns()
2525
{
2626
var campaignList = await _context.Campaigns
27-
.Include(c => c.Rules)
2827
.ToListAsync();
2928

29+
if (campaignList is null)
30+
{
31+
return Ok();
32+
}
33+
3034
var campaignDtoList = MapCampaignModelListToDtoList(campaignList);
3135

3236
return Ok(campaignDtoList);
@@ -36,7 +40,6 @@ public async Task<IActionResult> GetAllCampaigns()
3640
public async Task<IActionResult> GetCampaignById(int id)
3741
{
3842
var campaign = await _context.Campaigns
39-
.Include(c => c.Rules)
4043
.SingleOrDefaultAsync(c => c.Id == id);
4144

4245
if (campaign is null)
@@ -57,16 +60,16 @@ public async Task<IActionResult> CreateCampaign([FromBody] CampaignDTO campaignD
5760
return BadRequest();
5861
}
5962

60-
var campaingToCreate = MapCampaignDtoToModel(campaignDto);
63+
var campaign = MapCampaignDtoToModel(campaignDto);
6164

62-
await _context.Campaigns.AddAsync(campaingToCreate);
65+
await _context.Campaigns.AddAsync(campaign);
6366
await _context.SaveChangesAsync();
6467

65-
return CreatedAtAction(nameof(GetCampaignById), new { id = campaingToCreate.Id }, null);
68+
return CreatedAtAction(nameof(GetCampaignById), new { id = campaign.Id }, null);
6669
}
6770

6871
[HttpPut("{id:int}")]
69-
public async Task<IActionResult> UpdateCampaign(int id, [FromBody]CampaignDTO campaignDto)
72+
public async Task<IActionResult> UpdateCampaign(int id, [FromBody] CampaignDTO campaignDto)
7073
{
7174
if (id < 1 || campaignDto is null)
7275
{
@@ -123,68 +126,26 @@ private List<CampaignDTO> MapCampaignModelListToDtoList(List<Campaign> campaignL
123126

124127
private CampaignDTO MapCampaignModelToDto(Campaign campaign)
125128
{
126-
var campaignDto = new CampaignDTO
129+
return new CampaignDTO
127130
{
128131
Id = campaign.Id,
129132
Description = campaign.Description,
130133
From = campaign.From,
131134
To = campaign.To,
132135
Url = campaign.Url,
133136
};
134-
135-
campaign.Rules.ForEach(rule =>
136-
{
137-
var ruleDto = new RuleDTO
138-
{
139-
Id = rule.Id,
140-
RuleTypeId = rule.RuleTypeId,
141-
Description = rule.Description,
142-
CampaignId = rule.CampaignId
143-
};
144-
145-
switch (RuleType.From(rule.RuleTypeId))
146-
{
147-
case RuleTypeEnum.UserLocationRule:
148-
var userLocationRule = rule as UserLocationRule;
149-
ruleDto.LocationId = userLocationRule.LocationId;
150-
break;
151-
}
152-
153-
campaignDto.Rules.Add(ruleDto);
154-
});
155-
156-
return campaignDto;
157137
}
158138

159139
private Campaign MapCampaignDtoToModel(CampaignDTO campaignDto)
160140
{
161-
var campaingModel = new Campaign
141+
return new Campaign
162142
{
163143
Id = campaignDto.Id,
164144
Description = campaignDto.Description,
165145
From = campaignDto.From,
166146
To = campaignDto.To,
167147
Url = campaignDto.Url
168148
};
169-
170-
campaignDto.Rules.ForEach(ruleDto =>
171-
{
172-
switch (RuleType.From(ruleDto.RuleTypeId))
173-
{
174-
case RuleTypeEnum.UserLocationRule:
175-
campaingModel.Rules.Add(new UserLocationRule
176-
{
177-
Id = ruleDto.Id,
178-
LocationId = ruleDto.LocationId.Value,
179-
RuleTypeId = ruleDto.RuleTypeId,
180-
Description = ruleDto.Description,
181-
Campaign = campaingModel
182-
});
183-
break;
184-
}
185-
});
186-
187-
return campaingModel;
188149
}
189150
}
190151
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers
2+
{
3+
using Microsoft.AspNetCore.Authorization;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
6+
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure;
7+
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Threading.Tasks;
11+
12+
[Authorize]
13+
public class LocationsController : Controller
14+
{
15+
private readonly MarketingContext _context;
16+
17+
public LocationsController(MarketingContext context)
18+
{
19+
_context = context;
20+
}
21+
22+
[HttpGet]
23+
[Route("api/v1/campaigns/{campaignId:int}/locations/{userLocationRuleId:int}")]
24+
public IActionResult GetLocationByCampaignAndLocationRuleId(int campaignId,
25+
int userLocationRuleId)
26+
{
27+
if (campaignId < 1 || userLocationRuleId < 1)
28+
{
29+
return BadRequest();
30+
}
31+
32+
var location = _context.Rules
33+
.OfType<UserLocationRule>()
34+
.SingleOrDefault(c => c.CampaignId == campaignId && c.Id == userLocationRuleId);
35+
36+
if (location is null)
37+
{
38+
return NotFound();
39+
}
40+
41+
var locationDto = MapUserLocationRuleModelToDto(location);
42+
43+
return Ok(locationDto);
44+
}
45+
46+
[HttpGet]
47+
[Route("api/v1/campaigns/{campaignId:int}/locations")]
48+
public IActionResult GetAllLocationsByCampaignId(int campaignId)
49+
{
50+
if (campaignId < 1)
51+
{
52+
return BadRequest();
53+
}
54+
55+
var locationList = _context.Rules
56+
.OfType<UserLocationRule>()
57+
.Where(c => c.CampaignId == campaignId)
58+
.ToList();
59+
60+
if(locationList is null)
61+
{
62+
return Ok();
63+
}
64+
65+
var locationDtoList = MapUserLocationRuleModelListToDtoList(locationList);
66+
67+
return Ok(locationDtoList);
68+
}
69+
70+
[HttpPost]
71+
[Route("api/v1/campaigns/{campaignId:int}/locations")]
72+
public async Task<IActionResult> CreateLocation(int campaignId,
73+
[FromBody] UserLocationRuleDTO locationRuleDto)
74+
{
75+
if (campaignId < 1 || locationRuleDto is null)
76+
{
77+
return BadRequest();
78+
}
79+
80+
var locationRule = MapUserLocationRuleDtoToModel(locationRuleDto);
81+
locationRule.CampaignId = campaignId;
82+
83+
await _context.Rules.AddAsync(locationRule);
84+
await _context.SaveChangesAsync();
85+
86+
return CreatedAtAction(nameof(GetLocationByCampaignAndLocationRuleId),
87+
new { campaignId = campaignId, locationRuleId = locationRule.Id }, null);
88+
}
89+
90+
[HttpDelete]
91+
[Route("api/v1/campaigns/{campaignId:int}/locations/{userLocationRuleId:int}")]
92+
public async Task<IActionResult> DeleteLocationById(int campaignId, int userLocationRuleId)
93+
{
94+
if (campaignId < 1 || userLocationRuleId < 1)
95+
{
96+
return BadRequest();
97+
}
98+
99+
var locationToDelete = _context.Rules
100+
.OfType<UserLocationRule>()
101+
.SingleOrDefault(c => c.CampaignId == campaignId && c.Id == userLocationRuleId);
102+
103+
if (locationToDelete is null)
104+
{
105+
return NotFound();
106+
}
107+
108+
_context.Rules.Remove(locationToDelete);
109+
await _context.SaveChangesAsync();
110+
111+
return NoContent();
112+
}
113+
114+
115+
116+
private List<UserLocationRuleDTO> MapUserLocationRuleModelListToDtoList(List<UserLocationRule> userLocationRuleList)
117+
{
118+
var userLocationRuleDtoList = new List<UserLocationRuleDTO>();
119+
120+
userLocationRuleList.ForEach(userLocationRule => userLocationRuleDtoList
121+
.Add(MapUserLocationRuleModelToDto(userLocationRule)));
122+
123+
return userLocationRuleDtoList;
124+
}
125+
126+
private UserLocationRuleDTO MapUserLocationRuleModelToDto(UserLocationRule userLocationRule)
127+
{
128+
return new UserLocationRuleDTO
129+
{
130+
Id = userLocationRule.Id,
131+
Description = userLocationRule.Description,
132+
LocationId = userLocationRule.LocationId
133+
};
134+
}
135+
136+
private UserLocationRule MapUserLocationRuleDtoToModel(UserLocationRuleDTO userLocationRuleDto)
137+
{
138+
return new UserLocationRule
139+
{
140+
Id = userLocationRuleDto.Id,
141+
Description = userLocationRuleDto.Description,
142+
LocationId = userLocationRuleDto.LocationId
143+
};
144+
}
145+
}
146+
}
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
22
{
33
using System;
4-
using System.Collections.Generic;
54

65
public class CampaignDTO
76
{
@@ -14,12 +13,5 @@ public class CampaignDTO
1413
public DateTime To { get; set; }
1514

1615
public string Url { get; set; }
17-
18-
public List<RuleDTO> Rules { get; set; }
19-
20-
public CampaignDTO()
21-
{
22-
Rules = new List<RuleDTO>();
23-
}
2416
}
2517
}

src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
2+
{
3+
public class UserLocationRuleDTO
4+
{
5+
public int Id { get; set; }
6+
7+
public int LocationId { get; set; }
8+
9+
public string Description { get; set; }
10+
}
11+
}

src/Services/Marketing/Marketing.API/Infrastructure/MarketingContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ void ConfigureRules(EntityTypeBuilder<Rule> builder)
6464
.IsRequired();
6565

6666
builder.HasDiscriminator<int>("RuleTypeId")
67-
.HasValue<UserProfileRule>(1)
68-
.HasValue<PurchaseHistoryRule>(2)
69-
.HasValue<UserLocationRule>(3);
67+
.HasValue<UserProfileRule>((int)RuleTypeEnum.UserProfileRule)
68+
.HasValue<PurchaseHistoryRule>((int)RuleTypeEnum.PurchaseHistoryRule)
69+
.HasValue<UserLocationRule>((int)RuleTypeEnum.UserLocationRule);
7070

7171
builder.Property(r => r.Description)
7272
.HasColumnName("Description")

src/Services/Marketing/Marketing.API/Marketing.API.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="1.2.0" />
2020
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
2121
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
22+
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
2223
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
2324
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
2425
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="1.1.2" />
@@ -36,6 +37,8 @@
3637
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
3738
<PackageReference Include="Microsoft.Extensions.Options" Version="1.1.2" />
3839
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
40+
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" />
41+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" />
3942
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
4043
</ItemGroup>
4144
<ItemGroup>

src/Services/Marketing/Marketing.API/Model/Rule.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@ public abstract class Rule
44
{
55
public int Id { get; set; }
66

7-
public int RuleTypeId { get; set; }
8-
97
public int CampaignId { get; set; }
108

119
public Campaign Campaign { get; set; }
1210

1311
public string Description { get; set; }
12+
13+
public abstract int RuleTypeId { get;}
1414
}
1515

1616

1717
public class UserProfileRule : Rule
18-
{ }
18+
{
19+
public override int RuleTypeId => (int)RuleTypeEnum.UserProfileRule;
20+
}
1921

2022
public class PurchaseHistoryRule : Rule
21-
{ }
23+
{
24+
public override int RuleTypeId => (int)RuleTypeEnum.PurchaseHistoryRule;
25+
}
2226

2327
public class UserLocationRule : Rule
2428
{
29+
public override int RuleTypeId => (int)RuleTypeEnum.UserLocationRule;
30+
2531
public int LocationId { get; set; }
2632
}
2733
}

0 commit comments

Comments
 (0)