Browse Source

Redesign marketing campaign api in two different controllers (campaigns/locations)

pull/809/head
Christian Arenas 7 years ago
parent
commit
ce56a42bd1
9 changed files with 189 additions and 90 deletions
  1. +12
    -51
      src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs
  2. +146
    -0
      src/Services/Marketing/Marketing.API/Controllers/LocationsController.cs
  3. +0
    -8
      src/Services/Marketing/Marketing.API/Dto/CampaignDTO.cs
  4. +0
    -20
      src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs
  5. +11
    -0
      src/Services/Marketing/Marketing.API/Dto/UserLocationRuleDTO.cs
  6. +3
    -3
      src/Services/Marketing/Marketing.API/Infrastructure/MarketingContext.cs
  7. +3
    -0
      src/Services/Marketing/Marketing.API/Marketing.API.csproj
  8. +10
    -4
      src/Services/Marketing/Marketing.API/Model/Rule.cs
  9. +4
    -4
      src/Services/Marketing/Marketing.API/Model/RuleTypeEnum.cs

+ 12
- 51
src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs View File

@ -10,7 +10,7 @@
using Microsoft.AspNetCore.Authorization;
[Route("api/v1/[controller]")]
//[Authorize]
[Authorize]
public class CampaignsController : Controller
{
private readonly MarketingContext _context;
@ -24,9 +24,13 @@
public async Task<IActionResult> GetAllCampaigns()
{
var campaignList = await _context.Campaigns
.Include(c => c.Rules)
.ToListAsync();
if (campaignList is null)
{
return Ok();
}
var campaignDtoList = MapCampaignModelListToDtoList(campaignList);
return Ok(campaignDtoList);
@ -36,7 +40,6 @@
public async Task<IActionResult> GetCampaignById(int id)
{
var campaign = await _context.Campaigns
.Include(c => c.Rules)
.SingleOrDefaultAsync(c => c.Id == id);
if (campaign is null)
@ -57,16 +60,16 @@
return BadRequest();
}
var campaingToCreate = MapCampaignDtoToModel(campaignDto);
var campaign = MapCampaignDtoToModel(campaignDto);
await _context.Campaigns.AddAsync(campaingToCreate);
await _context.Campaigns.AddAsync(campaign);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetCampaignById), new { id = campaingToCreate.Id }, null);
return CreatedAtAction(nameof(GetCampaignById), new { id = campaign.Id }, null);
}
[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)
{
@ -123,7 +126,7 @@
private CampaignDTO MapCampaignModelToDto(Campaign campaign)
{
var campaignDto = new CampaignDTO
return new CampaignDTO
{
Id = campaign.Id,
Description = campaign.Description,
@ -131,34 +134,11 @@
To = campaign.To,
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)
{
var campaingModel = new Campaign
return new Campaign
{
Id = campaignDto.Id,
Description = campaignDto.Description,
@ -166,25 +146,6 @@
To = campaignDto.To,
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;
}
}
}

+ 146
- 0
src/Services/Marketing/Marketing.API/Controllers/LocationsController.cs View File

@ -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
};
}
}
}

+ 0
- 8
src/Services/Marketing/Marketing.API/Dto/CampaignDTO.cs View File

@ -1,7 +1,6 @@
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
{
using System;
using System.Collections.Generic;
public class CampaignDTO
{
@ -14,12 +13,5 @@
public DateTime To { get; set; }
public string Url { get; set; }
public List<RuleDTO> Rules { get; set; }
public CampaignDTO()
{
Rules = new List<RuleDTO>();
}
}
}

+ 0
- 20
src/Services/Marketing/Marketing.API/Dto/RuleDTO.cs View File

@ -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; }
}
}

+ 11
- 0
src/Services/Marketing/Marketing.API/Dto/UserLocationRuleDTO.cs View File

@ -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; }
}
}

+ 3
- 3
src/Services/Marketing/Marketing.API/Infrastructure/MarketingContext.cs View File

@ -64,9 +64,9 @@
.IsRequired();
builder.HasDiscriminator<int>("RuleTypeId")
.HasValue<UserProfileRule>(1)
.HasValue<PurchaseHistoryRule>(2)
.HasValue<UserLocationRule>(3);
.HasValue<UserProfileRule>((int)RuleTypeEnum.UserProfileRule)
.HasValue<PurchaseHistoryRule>((int)RuleTypeEnum.PurchaseHistoryRule)
.HasValue<UserLocationRule>((int)RuleTypeEnum.UserLocationRule);
builder.Property(r => r.Description)
.HasColumnName("Description")


+ 3
- 0
src/Services/Marketing/Marketing.API/Marketing.API.csproj View File

@ -19,6 +19,7 @@
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="1.2.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<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.Design" 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.Options" 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" />
</ItemGroup>
<ItemGroup>


+ 10
- 4
src/Services/Marketing/Marketing.API/Model/Rule.cs View File

@ -4,24 +4,30 @@
{
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 abstract int RuleTypeId { get;}
}
public class UserProfileRule : Rule
{ }
{
public override int RuleTypeId => (int)RuleTypeEnum.UserProfileRule;
}
public class PurchaseHistoryRule : Rule
{ }
{
public override int RuleTypeId => (int)RuleTypeEnum.PurchaseHistoryRule;
}
public class UserLocationRule : Rule
{
public override int RuleTypeId => (int)RuleTypeEnum.UserLocationRule;
public int LocationId { get; set; }
}
}

src/Services/Marketing/Marketing.API/Dto/RuleTypeEnum.cs → src/Services/Marketing/Marketing.API/Model/RuleTypeEnum.cs View File

@ -1,8 +1,8 @@
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
using System;
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Dto
namespace Microsoft.eShopOnContainers.Services.Marketing.API.Model
{
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Exceptions;
using System;
public enum RuleTypeEnum { UserProfileRule = 1, PurchaseHistoryRule = 2, UserLocationRule = 3 }
public static class RuleType

Loading…
Cancel
Save