@ -1,36 +1,86 @@ | |||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers | namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers | ||||
{ | { | ||||
using System.Collections.Generic; | |||||
using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure; | |||||
using System.Threading.Tasks; | |||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Model; | |||||
using Microsoft.EntityFrameworkCore; | |||||
[Route("api/[controller]")] | [Route("api/[controller]")] | ||||
public class CampaignsController : Controller | public class CampaignsController : Controller | ||||
{ | { | ||||
private readonly MarketingContext _context; | |||||
public CampaignsController(MarketingContext context) | |||||
{ | |||||
_context = context; | |||||
} | |||||
[HttpGet] | [HttpGet] | ||||
public IEnumerable<string> Get() | |||||
public async Task<IActionResult> GetAllCampaigns() | |||||
{ | { | ||||
return new string[] { "value1", "value2" }; | |||||
var campaignList = await _context.Campaigns | |||||
.Include(c => c.Rules) | |||||
.ToListAsync(); | |||||
return Ok(campaignList); | |||||
} | } | ||||
[HttpGet("{id:int}")] | [HttpGet("{id:int}")] | ||||
public string Get(int id) | |||||
public async Task<IActionResult> GetCampaignById(int id) | |||||
{ | { | ||||
return "value"; | |||||
var campaign = await _context.Campaigns | |||||
.Include(c => c.Rules) | |||||
.SingleAsync(c => c.Id == id); | |||||
if (campaign is null) | |||||
{ | |||||
return NotFound(); | |||||
} | |||||
return Ok(campaign); | |||||
} | } | ||||
[HttpPost] | [HttpPost] | ||||
public void Post([FromBody]string value) | |||||
public async Task<IActionResult> CreateCampaign([FromBody] Campaign campaign) | |||||
{ | { | ||||
await _context.Campaigns.AddAsync(campaign); | |||||
await _context.SaveChangesAsync(); | |||||
return CreatedAtAction(nameof(GetCampaignById), new { id = campaign.Id }, null); | |||||
} | } | ||||
[HttpPut("{id:int}")] | [HttpPut("{id:int}")] | ||||
public void Put(int id, [FromBody]string value) | |||||
public async Task<IActionResult> UpdateCampaign(int id, [FromBody]Campaign campaign) | |||||
{ | { | ||||
var campaignToUpdate = await _context.Campaigns.FindAsync(id); | |||||
if (campaign is null) | |||||
{ | |||||
return NotFound(); | |||||
} | |||||
campaignToUpdate.Description = campaign.Description; | |||||
campaignToUpdate.From = campaign.From; | |||||
campaignToUpdate.To = campaign.To; | |||||
await _context.SaveChangesAsync(); | |||||
return CreatedAtAction(nameof(GetCampaignById), new { id = campaignToUpdate.Id }, null); | |||||
} | } | ||||
[HttpDelete("{id:int}")] | [HttpDelete("{id:int}")] | ||||
public void Delete(int id) | |||||
public async Task<IActionResult> Delete(int id) | |||||
{ | { | ||||
var campaign = await _context.Campaigns.FindAsync(id); | |||||
if (campaign is null) | |||||
{ | |||||
return NotFound(); | |||||
} | |||||
_context.Campaigns.Remove(campaign); | |||||
await _context.SaveChangesAsync(); | |||||
return NoContent(); | |||||
} | } | ||||
} | } | ||||
} | |||||
} |
@ -0,0 +1,108 @@ | |||||
using System; | |||||
using Microsoft.EntityFrameworkCore; | |||||
using Microsoft.EntityFrameworkCore.Infrastructure; | |||||
using Microsoft.EntityFrameworkCore.Metadata; | |||||
using Microsoft.EntityFrameworkCore.Migrations; | |||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure; | |||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Migrations | |||||
{ | |||||
[DbContext(typeof(MarketingContext))] | |||||
[Migration("20170601175200_Initial")] | |||||
partial class Initial | |||||
{ | |||||
protected override void BuildTargetModel(ModelBuilder modelBuilder) | |||||
{ | |||||
modelBuilder | |||||
.HasAnnotation("ProductVersion", "1.1.2") | |||||
.HasAnnotation("SqlServer:Sequence:.campaign_hilo", "'campaign_hilo', '', '1', '10', '', '', 'Int64', 'False'") | |||||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Campaign", b => | |||||
{ | |||||
b.Property<int>("Id") | |||||
.ValueGeneratedOnAdd() | |||||
.HasAnnotation("SqlServer:HiLoSequenceName", "campaign_hilo") | |||||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.SequenceHiLo); | |||||
b.Property<string>("Description") | |||||
.IsRequired() | |||||
.HasColumnName("Description"); | |||||
b.Property<DateTime>("From") | |||||
.HasColumnName("From"); | |||||
b.Property<DateTime>("To") | |||||
.HasColumnName("To"); | |||||
b.Property<string>("Url"); | |||||
b.HasKey("Id"); | |||||
b.ToTable("Campaign"); | |||||
}); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Rule", b => | |||||
{ | |||||
b.Property<int>("Id") | |||||
.ValueGeneratedOnAdd(); | |||||
b.Property<int>("CampaignId"); | |||||
b.Property<string>("Description") | |||||
.IsRequired() | |||||
.HasColumnName("Description"); | |||||
b.Property<int>("RuleTypeId"); | |||||
b.HasKey("Id"); | |||||
b.HasIndex("CampaignId"); | |||||
b.ToTable("Rules"); | |||||
b.HasDiscriminator<int>("RuleTypeId"); | |||||
}); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.PurchaseHistoryRule", b => | |||||
{ | |||||
b.HasBaseType("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Rule"); | |||||
b.ToTable("PurchaseHistoryRule"); | |||||
b.HasDiscriminator().HasValue(2); | |||||
}); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.UserLocationRule", b => | |||||
{ | |||||
b.HasBaseType("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Rule"); | |||||
b.Property<int>("LocationId") | |||||
.HasColumnName("LocationId"); | |||||
b.ToTable("UserLocationRule"); | |||||
b.HasDiscriminator().HasValue(3); | |||||
}); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.UserProfileRule", b => | |||||
{ | |||||
b.HasBaseType("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Rule"); | |||||
b.ToTable("UserProfileRule"); | |||||
b.HasDiscriminator().HasValue(1); | |||||
}); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Rule", b => | |||||
{ | |||||
b.HasOne("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Campaign", "Campaign") | |||||
.WithMany("Rules") | |||||
.HasForeignKey("CampaignId") | |||||
.OnDelete(DeleteBehavior.Cascade); | |||||
}); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,71 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using Microsoft.EntityFrameworkCore.Migrations; | |||||
using Microsoft.EntityFrameworkCore.Metadata; | |||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Migrations | |||||
{ | |||||
public partial class Initial : Migration | |||||
{ | |||||
protected override void Up(MigrationBuilder migrationBuilder) | |||||
{ | |||||
migrationBuilder.CreateSequence( | |||||
name: "campaign_hilo", | |||||
incrementBy: 10); | |||||
migrationBuilder.CreateTable( | |||||
name: "Campaign", | |||||
columns: table => new | |||||
{ | |||||
Id = table.Column<int>(nullable: false), | |||||
Description = table.Column<string>(nullable: false), | |||||
From = table.Column<DateTime>(nullable: false), | |||||
To = table.Column<DateTime>(nullable: false), | |||||
Url = table.Column<string>(nullable: true) | |||||
}, | |||||
constraints: table => | |||||
{ | |||||
table.PrimaryKey("PK_Campaign", x => x.Id); | |||||
}); | |||||
migrationBuilder.CreateTable( | |||||
name: "Rules", | |||||
columns: table => new | |||||
{ | |||||
Id = table.Column<int>(nullable: false) | |||||
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), | |||||
CampaignId = table.Column<int>(nullable: false), | |||||
Description = table.Column<string>(nullable: false), | |||||
RuleTypeId = table.Column<int>(nullable: false), | |||||
LocationId = table.Column<int>(nullable: true) | |||||
}, | |||||
constraints: table => | |||||
{ | |||||
table.PrimaryKey("PK_Rules", x => x.Id); | |||||
table.ForeignKey( | |||||
name: "FK_Rules_Campaign_CampaignId", | |||||
column: x => x.CampaignId, | |||||
principalTable: "Campaign", | |||||
principalColumn: "Id", | |||||
onDelete: ReferentialAction.Cascade); | |||||
}); | |||||
migrationBuilder.CreateIndex( | |||||
name: "IX_Rules_CampaignId", | |||||
table: "Rules", | |||||
column: "CampaignId"); | |||||
} | |||||
protected override void Down(MigrationBuilder migrationBuilder) | |||||
{ | |||||
migrationBuilder.DropTable( | |||||
name: "Rules"); | |||||
migrationBuilder.DropTable( | |||||
name: "Campaign"); | |||||
migrationBuilder.DropSequence( | |||||
name: "campaign_hilo"); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,107 @@ | |||||
using System; | |||||
using Microsoft.EntityFrameworkCore; | |||||
using Microsoft.EntityFrameworkCore.Infrastructure; | |||||
using Microsoft.EntityFrameworkCore.Metadata; | |||||
using Microsoft.EntityFrameworkCore.Migrations; | |||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure; | |||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Migrations | |||||
{ | |||||
[DbContext(typeof(MarketingContext))] | |||||
partial class MarketingContextModelSnapshot : ModelSnapshot | |||||
{ | |||||
protected override void BuildModel(ModelBuilder modelBuilder) | |||||
{ | |||||
modelBuilder | |||||
.HasAnnotation("ProductVersion", "1.1.2") | |||||
.HasAnnotation("SqlServer:Sequence:.campaign_hilo", "'campaign_hilo', '', '1', '10', '', '', 'Int64', 'False'") | |||||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Campaign", b => | |||||
{ | |||||
b.Property<int>("Id") | |||||
.ValueGeneratedOnAdd() | |||||
.HasAnnotation("SqlServer:HiLoSequenceName", "campaign_hilo") | |||||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.SequenceHiLo); | |||||
b.Property<string>("Description") | |||||
.IsRequired() | |||||
.HasColumnName("Description"); | |||||
b.Property<DateTime>("From") | |||||
.HasColumnName("From"); | |||||
b.Property<DateTime>("To") | |||||
.HasColumnName("To"); | |||||
b.Property<string>("Url"); | |||||
b.HasKey("Id"); | |||||
b.ToTable("Campaign"); | |||||
}); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Rule", b => | |||||
{ | |||||
b.Property<int>("Id") | |||||
.ValueGeneratedOnAdd(); | |||||
b.Property<int>("CampaignId"); | |||||
b.Property<string>("Description") | |||||
.IsRequired() | |||||
.HasColumnName("Description"); | |||||
b.Property<int>("RuleTypeId"); | |||||
b.HasKey("Id"); | |||||
b.HasIndex("CampaignId"); | |||||
b.ToTable("Rules"); | |||||
b.HasDiscriminator<int>("RuleTypeId"); | |||||
}); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.PurchaseHistoryRule", b => | |||||
{ | |||||
b.HasBaseType("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Rule"); | |||||
b.ToTable("PurchaseHistoryRule"); | |||||
b.HasDiscriminator().HasValue(2); | |||||
}); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.UserLocationRule", b => | |||||
{ | |||||
b.HasBaseType("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Rule"); | |||||
b.Property<int>("LocationId") | |||||
.HasColumnName("LocationId"); | |||||
b.ToTable("UserLocationRule"); | |||||
b.HasDiscriminator().HasValue(3); | |||||
}); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.UserProfileRule", b => | |||||
{ | |||||
b.HasBaseType("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Rule"); | |||||
b.ToTable("UserProfileRule"); | |||||
b.HasDiscriminator().HasValue(1); | |||||
}); | |||||
modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Rule", b => | |||||
{ | |||||
b.HasOne("Microsoft.eShopOnContainers.Services.Marketing.API.Model.Campaign", "Campaign") | |||||
.WithMany("Rules") | |||||
.HasForeignKey("CampaignId") | |||||
.OnDelete(DeleteBehavior.Cascade); | |||||
}); | |||||
} | |||||
} | |||||
} |
@ -1,7 +1,20 @@ | |||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Model | namespace Microsoft.eShopOnContainers.Services.Marketing.API.Model | ||||
{ | { | ||||
public class Campaing | |||||
using System; | |||||
using System.Collections.Generic; | |||||
public class Campaign | |||||
{ | { | ||||
//TODO: Add Campaing properties | |||||
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 ICollection<Rule> Rules { get; set; } | |||||
} | } | ||||
} | |||||
} |
@ -0,0 +1,9 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Model | |||||
{ | |||||
public class Location | |||||
{ | |||||
public int Id { get; set; } | |||||
} | |||||
} |
@ -0,0 +1,29 @@ | |||||
using System.Collections.Generic; | |||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Model | |||||
{ | |||||
public abstract class Rule | |||||
{ | |||||
public int Id { get; set; } | |||||
public int RuleTypeId { get; set; } | |||||
public int CampaignId { get; set; } | |||||
public Campaign Campaign { get; set; } | |||||
public string Description { get; set; } | |||||
} | |||||
public class UserProfileRule : Rule | |||||
{ } | |||||
public class PurchaseHistoryRule : Rule | |||||
{ } | |||||
public class UserLocationRule : Rule | |||||
{ | |||||
public int LocationId { get; set; } | |||||
} | |||||
} |