Skip to content

Commit 32247c5

Browse files
committed
Add marketing api
1 parent 665733c commit 32247c5

19 files changed

Lines changed: 566 additions & 96 deletions

docker-compose.override.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ services:
4949
ports:
5050
- "5102:80"
5151

52+
marketing.api:
53+
environment:
54+
- ASPNETCORE_ENVIRONMENT=Development
55+
- ASPNETCORE_URLS=http://0.0.0.0:80
56+
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.MarketingDb;User Id=sa;Password=Pass@word
57+
- identityUrl=http://identity.api #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
58+
ports:
59+
- "5110:80"
60+
5261
webspa:
5362
environment:
5463
- ASPNETCORE_ENVIRONMENT=Development
@@ -94,13 +103,4 @@ services:
94103
- mvc=http://webmvc/hc
95104
- spa=http://webspa/hc
96105
ports:
97-
- "5107:80"
98-
99-
marketing.api:
100-
environment:
101-
- ASPNETCORE_ENVIRONMENT=Development
102-
- ASPNETCORE_URLS=http://0.0.0.0:80
103-
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.MarketingDb;User Id=sa;Password=Pass@word
104-
- identityUrl=http://identity.api #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
105-
ports:
106-
- "5110:80"
106+
- "5107:80"

docker-compose.prod.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ services:
5454
ports:
5555
- "5102:80"
5656

57+
marketing.api:
58+
environment:
59+
- ASPNETCORE_ENVIRONMENT=Production
60+
- ASPNETCORE_URLS=http://0.0.0.0:80
61+
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.MarketingDb;User Id=sa;Password=Pass@word
62+
- identityUrl=http://identity.api #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
63+
ports:
64+
- "5110:80"
65+
5766
webspa:
5867
environment:
5968
- ASPNETCORE_ENVIRONMENT=Production
@@ -99,13 +108,4 @@ services:
99108
- spa=http://webspa/hc
100109

101110
ports:
102-
- "5107:80"
103-
104-
marketing.api:
105-
environment:
106-
- ASPNETCORE_ENVIRONMENT=Production
107-
- ASPNETCORE_URLS=http://0.0.0.0:80
108-
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.MarketingDb;User Id=sa;Password=Pass@word
109-
- identityUrl=http://identity.api #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
110-
ports:
111-
- "5110:80"
111+
- "5107:80"

docker-compose.vs.debug.yml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,62 +61,64 @@ services:
6161
labels:
6262
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
6363

64-
webspa:
65-
image: eshop/webspa:dev
64+
marketing.api:
65+
image: eshop/marketing.api:dev
6666
build:
6767
args:
6868
source: ${DOCKER_BUILD_SOURCE}
6969
environment:
7070
- DOTNET_USE_POLLING_FILE_WATCHER=1
7171
volumes:
72-
- ./src/Web/WebSPA:/app
72+
- ./src/Services/Marketing/Marketing.API:/app
7373
- ~/.nuget/packages:/root/.nuget/packages:ro
7474
- ~/clrdbg:/clrdbg:ro
7575
entrypoint: tail -f /dev/null
7676
labels:
7777
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
7878

79-
webmvc:
80-
image: eshop/webmvc:dev
79+
80+
webspa:
81+
image: eshop/webspa:dev
8182
build:
8283
args:
8384
source: ${DOCKER_BUILD_SOURCE}
8485
environment:
8586
- DOTNET_USE_POLLING_FILE_WATCHER=1
8687
volumes:
87-
- ./src/Web/WebMVC:/app
88+
- ./src/Web/WebSPA:/app
8889
- ~/.nuget/packages:/root/.nuget/packages:ro
8990
- ~/clrdbg:/clrdbg:ro
9091
entrypoint: tail -f /dev/null
9192
labels:
9293
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
9394

94-
webstatus:
95-
image: eshop/webstatus:dev
95+
webmvc:
96+
image: eshop/webmvc:dev
9697
build:
9798
args:
9899
source: ${DOCKER_BUILD_SOURCE}
99100
environment:
100101
- DOTNET_USE_POLLING_FILE_WATCHER=1
101102
volumes:
102-
- ./src/Web/WebStatus:/app
103+
- ./src/Web/WebMVC:/app
103104
- ~/.nuget/packages:/root/.nuget/packages:ro
104105
- ~/clrdbg:/clrdbg:ro
105106
entrypoint: tail -f /dev/null
106107
labels:
107108
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
108109

109-
marketing.api:
110-
image: eshop/marketing.api:dev
110+
webstatus:
111+
image: eshop/webstatus:dev
111112
build:
112113
args:
113114
source: ${DOCKER_BUILD_SOURCE}
114115
environment:
115116
- DOTNET_USE_POLLING_FILE_WATCHER=1
116117
volumes:
117-
- ./src/Marketing/Marketing.API:/app
118+
- ./src/Web/WebStatus:/app
118119
- ~/.nuget/packages:/root/.nuget/packages:ro
119120
- ~/clrdbg:/clrdbg:ro
120121
entrypoint: tail -f /dev/null
121122
labels:
122123
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
124+

docker-compose.vs.release.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ services:
4141
labels:
4242
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
4343

44-
webspa:
44+
marketing.api:
4545
build:
4646
args:
4747
source: ${DOCKER_BUILD_SOURCE}
@@ -51,7 +51,7 @@ services:
5151
labels:
5252
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
5353

54-
webmvc:
54+
webspa:
5555
build:
5656
args:
5757
source: ${DOCKER_BUILD_SOURCE}
@@ -61,7 +61,7 @@ services:
6161
labels:
6262
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
6363

64-
webstatus:
64+
webmvc:
6565
build:
6666
args:
6767
source: ${DOCKER_BUILD_SOURCE}
@@ -71,12 +71,12 @@ services:
7171
labels:
7272
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
7373

74-
marketing.api:
74+
webstatus:
7575
build:
7676
args:
7777
source: ${DOCKER_BUILD_SOURCE}
7878
volumes:
7979
- ~/clrdbg:/clrdbg:ro
8080
entrypoint: tail -f /dev/null
8181
labels:
82-
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
82+
- "com.microsoft.visualstudio.targetoperatingsystem=linux"
Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,86 @@
11
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers
22
{
3-
using System.Collections.Generic;
43
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure;
5+
using System.Threading.Tasks;
6+
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
7+
using Microsoft.EntityFrameworkCore;
58

69
[Route("api/[controller]")]
710
public class CampaignsController : Controller
811
{
12+
private readonly MarketingContext _context;
13+
14+
public CampaignsController(MarketingContext context)
15+
{
16+
_context = context;
17+
}
18+
919
[HttpGet]
10-
public IEnumerable<string> Get()
20+
public async Task<IActionResult> GetAllCampaigns()
1121
{
12-
return new string[] { "value1", "value2" };
22+
var campaignList = await _context.Campaigns
23+
.Include(c => c.Rules)
24+
.ToListAsync();
25+
26+
return Ok(campaignList);
1327
}
1428

1529
[HttpGet("{id:int}")]
16-
public string Get(int id)
30+
public async Task<IActionResult> GetCampaignById(int id)
1731
{
18-
return "value";
32+
var campaign = await _context.Campaigns
33+
.Include(c => c.Rules)
34+
.SingleAsync(c => c.Id == id);
35+
36+
if (campaign is null)
37+
{
38+
return NotFound();
39+
}
40+
41+
return Ok(campaign);
1942
}
2043

2144
[HttpPost]
22-
public void Post([FromBody]string value)
45+
public async Task<IActionResult> CreateCampaign([FromBody] Campaign campaign)
2346
{
47+
await _context.Campaigns.AddAsync(campaign);
48+
await _context.SaveChangesAsync();
49+
50+
return CreatedAtAction(nameof(GetCampaignById), new { id = campaign.Id }, null);
2451
}
2552

2653
[HttpPut("{id:int}")]
27-
public void Put(int id, [FromBody]string value)
54+
public async Task<IActionResult> UpdateCampaign(int id, [FromBody]Campaign campaign)
2855
{
56+
var campaignToUpdate = await _context.Campaigns.FindAsync(id);
57+
if (campaign is null)
58+
{
59+
return NotFound();
60+
}
61+
62+
campaignToUpdate.Description = campaign.Description;
63+
campaignToUpdate.From = campaign.From;
64+
campaignToUpdate.To = campaign.To;
65+
66+
await _context.SaveChangesAsync();
67+
68+
return CreatedAtAction(nameof(GetCampaignById), new { id = campaignToUpdate.Id }, null);
2969
}
3070

3171
[HttpDelete("{id:int}")]
32-
public void Delete(int id)
72+
public async Task<IActionResult> Delete(int id)
3373
{
74+
var campaign = await _context.Campaigns.FindAsync(id);
75+
if (campaign is null)
76+
{
77+
return NotFound();
78+
}
79+
80+
_context.Campaigns.Remove(campaign);
81+
await _context.SaveChangesAsync();
82+
83+
return NoContent();
3484
}
3585
}
36-
}
86+
}

src/Services/Marketing/Marketing.API/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM microsoft/aspnetcore:1.1
1+
FROM microsoft/aspnetcore:1.1.2
22
ARG source
33
WORKDIR /app
44
EXPOSE 80

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

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,77 @@
66

77
public class MarketingContext : DbContext
88
{
9-
public MarketingContext(DbContextOptions options) : base(options)
10-
{
9+
public MarketingContext(DbContextOptions<MarketingContext> options) : base(options)
10+
{
1111
}
1212

13-
public DbSet<Campaing> Campaings { get; set; }
13+
public DbSet<Campaign> Campaigns { get; set; }
14+
15+
public DbSet<Rule> Rules { get; set; }
16+
public DbSet<UserProfileRule> UserProfileRules { get; set; }
17+
public DbSet<PurchaseHistoryRule> PurchaseHistoryRules { get; set; }
18+
public DbSet<UserLocationRule> UserLocationRules { get; set; }
1419

1520
protected override void OnModelCreating(ModelBuilder builder)
1621
{
17-
builder.Entity<Campaing>(ConfigureCampaings);
22+
builder.Entity<Campaign>(ConfigureCampaigns);
23+
builder.Entity<Rule>(ConfigureRules);
24+
builder.Entity<UserLocationRule>(ConfigureUserLocationRules);
1825
}
1926

20-
void ConfigureCampaings(EntityTypeBuilder<Campaing> builder)
27+
void ConfigureCampaigns(EntityTypeBuilder<Campaign> builder)
2128
{
29+
builder.ToTable("Campaign");
30+
31+
builder.HasKey(ci => ci.Id);
32+
33+
builder.Property(ci => ci.Id)
34+
.ForSqlServerUseSequenceHiLo("campaign_hilo")
35+
.IsRequired();
36+
37+
builder.Property(m => m.Description)
38+
.HasColumnName("Description")
39+
.IsRequired();
2240

41+
builder.Property(m => m.From)
42+
.HasColumnName("From")
43+
.IsRequired();
44+
45+
builder.Property(m => m.To)
46+
.HasColumnName("To")
47+
.IsRequired();
48+
49+
builder.Property(m => m.Description)
50+
.HasColumnName("Description")
51+
.IsRequired();
52+
53+
builder.HasMany(m => m.Rules)
54+
.WithOne(r => r.Campaign)
55+
.HasForeignKey(m => m.CampaignId)
56+
.IsRequired();
57+
}
58+
59+
void ConfigureRules(EntityTypeBuilder<Rule> builder)
60+
{
61+
builder.ToTable("Rules");
62+
63+
builder.HasKey(r => r.Id);
64+
65+
builder.HasDiscriminator<int>("RuleTypeId")
66+
.HasValue<UserProfileRule>(1)
67+
.HasValue<PurchaseHistoryRule>(2)
68+
.HasValue<UserLocationRule>(3);
69+
70+
builder.Property(r => r.Description)
71+
.HasColumnName("Description")
72+
.IsRequired();
73+
}
74+
75+
void ConfigureUserLocationRules(EntityTypeBuilder<UserLocationRule> builder)
76+
{
77+
builder.Property(r => r.LocationId)
78+
.HasColumnName("LocationId")
79+
.IsRequired();
2380
}
2481
}
25-
}
82+
}

0 commit comments

Comments
 (0)