Redesign marketing campaign api in two different controllers (campaigns/locations)
This commit is contained in:
parent
31b1c4a197
commit
99685b3eae
@ -10,7 +10,7 @@
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
[Route("api/v1/[controller]")]
|
[Route("api/v1/[controller]")]
|
||||||
//[Authorize]
|
[Authorize]
|
||||||
public class CampaignsController : Controller
|
public class CampaignsController : Controller
|
||||||
{
|
{
|
||||||
private readonly MarketingContext _context;
|
private readonly MarketingContext _context;
|
||||||
@ -24,9 +24,13 @@
|
|||||||
public async Task<IActionResult> GetAllCampaigns()
|
public async Task<IActionResult> GetAllCampaigns()
|
||||||
{
|
{
|
||||||
var campaignList = await _context.Campaigns
|
var campaignList = await _context.Campaigns
|
||||||
.Include(c => c.Rules)
|
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
|
if (campaignList is null)
|
||||||
|
{
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
var campaignDtoList = MapCampaignModelListToDtoList(campaignList);
|
var campaignDtoList = MapCampaignModelListToDtoList(campaignList);
|
||||||
|
|
||||||
return Ok(campaignDtoList);
|
return Ok(campaignDtoList);
|
||||||
@ -36,7 +40,6 @@
|
|||||||
public async Task<IActionResult> GetCampaignById(int id)
|
public async Task<IActionResult> GetCampaignById(int id)
|
||||||
{
|
{
|
||||||
var campaign = await _context.Campaigns
|
var campaign = await _context.Campaigns
|
||||||
.Include(c => c.Rules)
|
|
||||||
.SingleOrDefaultAsync(c => c.Id == id);
|
.SingleOrDefaultAsync(c => c.Id == id);
|
||||||
|
|
||||||
if (campaign is null)
|
if (campaign is null)
|
||||||
@ -57,16 +60,16 @@
|
|||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
var campaingToCreate = MapCampaignDtoToModel(campaignDto);
|
var campaign = MapCampaignDtoToModel(campaignDto);
|
||||||
|
|
||||||
await _context.Campaigns.AddAsync(campaingToCreate);
|
await _context.Campaigns.AddAsync(campaign);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
return CreatedAtAction(nameof(GetCampaignById), new { id = campaingToCreate.Id }, null);
|
return CreatedAtAction(nameof(GetCampaignById), new { id = campaign.Id }, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{id:int}")]
|
[HttpPut("{id:int}")]
|
||||||
public async Task<IActionResult> UpdateCampaign(int id, [FromBody]CampaignDTO campaignDto)
|
public async Task<IActionResult> UpdateCampaign(int id, [FromBody] CampaignDTO campaignDto)
|
||||||
{
|
{
|
||||||
if (id < 1 || campaignDto is null)
|
if (id < 1 || campaignDto is null)
|
||||||
{
|
{
|
||||||
@ -123,7 +126,7 @@
|
|||||||
|
|
||||||
private CampaignDTO MapCampaignModelToDto(Campaign campaign)
|
private CampaignDTO MapCampaignModelToDto(Campaign campaign)
|
||||||
{
|
{
|
||||||
var campaignDto = new CampaignDTO
|
return new CampaignDTO
|
||||||
{
|
{
|
||||||
Id = campaign.Id,
|
Id = campaign.Id,
|
||||||
Description = campaign.Description,
|
Description = campaign.Description,
|
||||||
@ -131,34 +134,11 @@
|
|||||||
To = campaign.To,
|
To = campaign.To,
|
||||||
Url = campaign.Url,
|
Url = campaign.Url,
|
||||||
};
|
};
|
||||||
|
|
||||||
campaign.Rules.ForEach(rule =>
|
|
||||||
{
|
|
||||||
var ruleDto = new RuleDTO
|
|
||||||
{
|
|
||||||
Id = rule.Id,
|
|
||||||
RuleTypeId = rule.RuleTypeId,
|
|
||||||
Description = rule.Description,
|
|
||||||
CampaignId = rule.CampaignId
|
|
||||||
};
|
|
||||||
|
|
||||||
switch (RuleType.From(rule.RuleTypeId))
|
|
||||||
{
|
|
||||||
case RuleTypeEnum.UserLocationRule:
|
|
||||||
var userLocationRule = rule as UserLocationRule;
|
|
||||||
ruleDto.LocationId = userLocationRule.LocationId;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
campaignDto.Rules.Add(ruleDto);
|
|
||||||
});
|
|
||||||
|
|
||||||
return campaignDto;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Campaign MapCampaignDtoToModel(CampaignDTO campaignDto)
|
private Campaign MapCampaignDtoToModel(CampaignDTO campaignDto)
|
||||||
{
|
{
|
||||||
var campaingModel = new Campaign
|
return new Campaign
|
||||||
{
|
{
|
||||||
Id = campaignDto.Id,
|
Id = campaignDto.Id,
|
||||||
Description = campaignDto.Description,
|
Description = campaignDto.Description,
|
||||||
@ -166,25 +146,6 @@
|
|||||||
To = campaignDto.To,
|
To = campaignDto.To,
|
||||||
Url = campaignDto.Url
|
Url = campaignDto.Url
|
||||||
};
|
};
|
||||||
|
|
||||||
campaignDto.Rules.ForEach(ruleDto =>
|
|
||||||
{
|
|
||||||
switch (RuleType.From(ruleDto.RuleTypeId))
|
|
||||||
{
|
|
||||||
case RuleTypeEnum.UserLocationRule:
|
|
||||||
campaingModel.Rules.Add(new UserLocationRule
|
|
||||||
{
|
|
||||||
Id = ruleDto.Id,
|
|
||||||
LocationId = ruleDto.LocationId.Value,
|
|
||||||
RuleTypeId = ruleDto.RuleTypeId,
|
|
||||||
Description = ruleDto.Description,
|
|
||||||
Campaign = campaingModel
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return campaingModel;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,146 @@
|
|||||||
|
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers
|
||||||
|
{
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Marketing.API.Dto;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
public class LocationsController : Controller
|
||||||
|
{
|
||||||
|
private readonly MarketingContext _context;
|
||||||
|
|
||||||
|
public LocationsController(MarketingContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[Route("api/v1/campaigns/{campaignId:int}/locations/{userLocationRuleId:int}")]
|
||||||
|
public IActionResult GetLocationByCampaignAndLocationRuleId(int campaignId,
|
||||||
|
int userLocationRuleId)
|
||||||
|
{
|
||||||
|
if (campaignId < 1 || userLocationRuleId < 1)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
var location = _context.Rules
|
||||||
|
.OfType<UserLocationRule>()
|
||||||
|
.SingleOrDefault(c => c.CampaignId == campaignId && c.Id == userLocationRuleId);
|
||||||
|
|
||||||
|
if (location is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
var locationDto = MapUserLocationRuleModelToDto(location);
|
||||||
|
|
||||||
|
return Ok(locationDto);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[Route("api/v1/campaigns/{campaignId:int}/locations")]
|
||||||
|
public IActionResult GetAllLocationsByCampaignId(int campaignId)
|
||||||
|
{
|
||||||
|
if (campaignId < 1)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
var locationList = _context.Rules
|
||||||
|
.OfType<UserLocationRule>()
|
||||||
|
.Where(c => c.CampaignId == campaignId)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if(locationList is null)
|
||||||
|
{
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
var locationDtoList = MapUserLocationRuleModelListToDtoList(locationList);
|
||||||
|
|
||||||
|
return Ok(locationDtoList);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[Route("api/v1/campaigns/{campaignId:int}/locations")]
|
||||||
|
public async Task<IActionResult> CreateLocation(int campaignId,
|
||||||
|
[FromBody] UserLocationRuleDTO locationRuleDto)
|
||||||
|
{
|
||||||
|
if (campaignId < 1 || locationRuleDto is null)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
var locationRule = MapUserLocationRuleDtoToModel(locationRuleDto);
|
||||||
|
locationRule.CampaignId = campaignId;
|
||||||
|
|
||||||
|
await _context.Rules.AddAsync(locationRule);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return CreatedAtAction(nameof(GetLocationByCampaignAndLocationRuleId),
|
||||||
|
new { campaignId = campaignId, locationRuleId = locationRule.Id }, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete]
|
||||||
|
[Route("api/v1/campaigns/{campaignId:int}/locations/{userLocationRuleId:int}")]
|
||||||
|
public async Task<IActionResult> DeleteLocationById(int campaignId, int userLocationRuleId)
|
||||||
|
{
|
||||||
|
if (campaignId < 1 || userLocationRuleId < 1)
|
||||||
|
{
|
||||||
|
return BadRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
var locationToDelete = _context.Rules
|
||||||
|
.OfType<UserLocationRule>()
|
||||||
|
.SingleOrDefault(c => c.CampaignId == campaignId && c.Id == userLocationRuleId);
|
||||||
|
|
||||||
|
if (locationToDelete is null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
_context.Rules.Remove(locationToDelete);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private List<UserLocationRuleDTO> MapUserLocationRuleModelListToDtoList(List<UserLocationRule> userLocationRuleList)
|
||||||
|
{
|
||||||
|
var userLocationRuleDtoList = new List<UserLocationRuleDTO>();
|
||||||
|
|
||||||
|
userLocationRuleList.ForEach(userLocationRule => userLocationRuleDtoList
|
||||||
|
.Add(MapUserLocationRuleModelToDto(userLocationRule)));
|
||||||
|
|
||||||
|
return userLocationRuleDtoList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserLocationRuleDTO MapUserLocationRuleModelToDto(UserLocationRule userLocationRule)
|
||||||
|
{
|
||||||
|
return new UserLocationRuleDTO
|
||||||
|
{
|
||||||
|
Id = userLocationRule.Id,
|
||||||
|
Description = userLocationRule.Description,
|
||||||
|
LocationId = userLocationRule.LocationId
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserLocationRule MapUserLocationRuleDtoToModel(UserLocationRuleDTO userLocationRuleDto)
|
||||||
|
{
|
||||||
|
return new UserLocationRule
|
||||||
|
{
|
||||||
|
Id = userLocationRuleDto.Id,
|
||||||
|
Description = userLocationRuleDto.Description,
|
||||||
|
LocationId = userLocationRuleDto.LocationId
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
|
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
|
||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
public class CampaignDTO
|
public class CampaignDTO
|
||||||
{
|
{
|
||||||
@ -14,12 +13,5 @@
|
|||||||
public DateTime To { get; set; }
|
public DateTime To { get; set; }
|
||||||
|
|
||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
|
|
||||||
public List<RuleDTO> Rules { get; set; }
|
|
||||||
|
|
||||||
public CampaignDTO()
|
|
||||||
{
|
|
||||||
Rules = new List<RuleDTO>();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,20 +0,0 @@
|
|||||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
|
|
||||||
{
|
|
||||||
public class RuleDTO
|
|
||||||
{
|
|
||||||
public int Id { get; set; }
|
|
||||||
|
|
||||||
public int RuleTypeId { get; set; }
|
|
||||||
|
|
||||||
public int CampaignId { get; set; }
|
|
||||||
|
|
||||||
public int? LocationId { get; set; }
|
|
||||||
|
|
||||||
public string Description { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,11 @@
|
|||||||
|
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
|
||||||
|
{
|
||||||
|
public class UserLocationRuleDTO
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public int LocationId { get; set; }
|
||||||
|
|
||||||
|
public string Description { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -64,9 +64,9 @@
|
|||||||
.IsRequired();
|
.IsRequired();
|
||||||
|
|
||||||
builder.HasDiscriminator<int>("RuleTypeId")
|
builder.HasDiscriminator<int>("RuleTypeId")
|
||||||
.HasValue<UserProfileRule>(1)
|
.HasValue<UserProfileRule>((int)RuleTypeEnum.UserProfileRule)
|
||||||
.HasValue<PurchaseHistoryRule>(2)
|
.HasValue<PurchaseHistoryRule>((int)RuleTypeEnum.PurchaseHistoryRule)
|
||||||
.HasValue<UserLocationRule>(3);
|
.HasValue<UserLocationRule>((int)RuleTypeEnum.UserLocationRule);
|
||||||
|
|
||||||
builder.Property(r => r.Description)
|
builder.Property(r => r.Description)
|
||||||
.HasColumnName("Description")
|
.HasColumnName("Description")
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="1.2.0" />
|
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="1.2.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
|
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.2" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="1.1.2" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="1.1.2" />
|
||||||
@ -36,6 +37,8 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Options" Version="1.1.2" />
|
<PackageReference Include="Microsoft.Extensions.Options" Version="1.1.2" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
|
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
|
||||||
|
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.2" />
|
||||||
|
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -4,24 +4,30 @@
|
|||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
public int RuleTypeId { get; set; }
|
|
||||||
|
|
||||||
public int CampaignId { get; set; }
|
public int CampaignId { get; set; }
|
||||||
|
|
||||||
public Campaign Campaign { get; set; }
|
public Campaign Campaign { get; set; }
|
||||||
|
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
|
|
||||||
|
public abstract int RuleTypeId { get;}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class UserProfileRule : Rule
|
public class UserProfileRule : Rule
|
||||||
{ }
|
{
|
||||||
|
public override int RuleTypeId => (int)RuleTypeEnum.UserProfileRule;
|
||||||
|
}
|
||||||
|
|
||||||
public class PurchaseHistoryRule : Rule
|
public class PurchaseHistoryRule : Rule
|
||||||
{ }
|
{
|
||||||
|
public override int RuleTypeId => (int)RuleTypeEnum.PurchaseHistoryRule;
|
||||||
|
}
|
||||||
|
|
||||||
public class UserLocationRule : Rule
|
public class UserLocationRule : Rule
|
||||||
{
|
{
|
||||||
|
public override int RuleTypeId => (int)RuleTypeEnum.UserLocationRule;
|
||||||
|
|
||||||
public int LocationId { get; set; }
|
public int LocationId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
|
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Model
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
|
|
||||||
{
|
{
|
||||||
|
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
|
||||||
|
using System;
|
||||||
|
|
||||||
public enum RuleTypeEnum { UserProfileRule = 1, PurchaseHistoryRule = 2, UserLocationRule = 3 }
|
public enum RuleTypeEnum { UserProfileRule = 1, PurchaseHistoryRule = 2, UserLocationRule = 3 }
|
||||||
|
|
||||||
public static class RuleType
|
public static class RuleType
|
Loading…
x
Reference in New Issue
Block a user