Created
September 24, 2021 17:53
-
-
Save DocGreenRob/aca57a0613d2f07364606dbed1ef281b to your computer and use it in GitHub Desktop.
Proper Controller Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Cge.Core.Extensions; | |
using Cge.Core.Logging; | |
using Cge.Core.Models; | |
using HomeaZZon.Common.Dto; | |
using HomeaZZon.Common.Enum; | |
using HomeaZZon.Manager; | |
using HomeaZZon.Manager.Interfaces; | |
using HomeaZZon.ServiceBus; | |
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Configuration; | |
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
namespace HomeaZZon.Api.Controllers | |
{ | |
[ApiController] | |
[Authorize] | |
public class AmazonController : BaseController | |
{ | |
private readonly IAmazonManager _amazonManager; | |
private readonly IMessagePublisher _messagePublisher; | |
public AmazonController(IApplicationContext applicationContext, | |
ITelemetryClient telemetryClient, | |
IConfiguration configuration, | |
IAmazonManager amazonManager, | |
IMessagePublisher messagePublisher) : base(telemetryClient, | |
configuration, | |
applicationContext) | |
{ | |
_amazonManager = amazonManager.ValidateArgNotNull(nameof(amazonManager)); | |
_messagePublisher = messagePublisher.ValidateArgNotNull(nameof(messagePublisher)); | |
} | |
#region Swagger Documentation | |
/// <summary> | |
/// Does a soft delete on the Amazon, Payload, ArtifactIndex, and AssetInfo tables | |
/// </summary> | |
/// <remarks> | |
/// Sample request: | |
/// | |
/// DELETE /api/amazon/1 | |
/// | |
/// </remarks> | |
/// <response code="200">Ok()</response> | |
#endregion | |
[HttpDelete] | |
[Route("api/amazon/{id}")] | |
[ProducesResponseType(200)] | |
public async Task<ActionResult> DeleteAsync(long id) | |
{ | |
await _amazonManager.DeleteAsync(id).ConfigureAwait(false); | |
return Ok(); | |
} | |
#region Swagger Documentation | |
/// <summary> | |
/// Get the Amazon Product from the Db | |
/// </summary> | |
/// <remarks> | |
/// Sample request: | |
/// | |
/// GET /api/amazon/1 | |
/// | |
/// Sample response: | |
/// | |
/// { | |
/// "id": 1, | |
/// "name": "Bare Decor EZ-Floor Interlocking Flooring Tiles in Solid Teak Wood Oiled Finish (Set of 10), Long 9 Slat", | |
/// "assetId": 14, | |
/// "assetInfo": { | |
/// "id": 14, | |
/// "price": 0, | |
/// "title": "Bare Decor EZ-Floor Interlocking Flooring Tiles in Solid Teak Wood Oiled Finish (Set of 10), Long 9 Slat", | |
/// "totalQuantity": 0, | |
/// "type": "Amazon", | |
/// "typeId": 1 | |
/// }, | |
/// "image": "https://m.media-amazon.com/images/I/816a+lQ1w-L._AC_UL320_ML3_.jpg", | |
/// "link": "https://www.amazon.com/Bare-Decor-EZ-Floor-Interlocking-Flooring/dp/B014S7JPCO/ref=sr_1_3?keywords=Wood+floors&qid=1580015248&sr=8-3", | |
/// "price": "83.99", | |
/// "isUnopenedSuggestion": false, | |
/// "isMetattach": false, | |
/// "isMy": false, | |
/// "isSuggest": true, | |
/// "isWishlist": false, | |
/// "type": "Amazon" | |
/// } | |
/// </remarks> | |
/// <returns>The Amazon product from the Db</returns> | |
/// <response code="200">Returns the Amazon product from the Db</response> | |
#endregion | |
[HttpGet] | |
[Route("api/amazon/{id}")] | |
[ProducesResponseType(200)] | |
public async Task<ActionResult> GetAmazonAsync(long id = 1) | |
{ | |
var result = await _amazonManager.GetAmazonAsync(id).ConfigureAwait(false); | |
return Ok(result); | |
} | |
#region Swagger Documentation | |
/// <summary> | |
/// Get Amazon Products from the Db | |
/// </summary> | |
/// <remarks> | |
/// Sample request: | |
/// | |
/// GET /api/amazon/4/0/36/0 | |
/// | |
/// Sample response: | |
/// | |
/// [ | |
/// { | |
/// "id": 1, | |
/// "name": "Bare Decor EZ-Floor Interlocking Flooring Tiles in Solid Teak Wood Oiled Finish (Set of 10), Long 9 Slat", | |
/// "assetId": 14, | |
/// "assetInfo": { | |
/// "id": 14, | |
/// "price": 0, | |
/// "title": "Bare Decor EZ-Floor Interlocking Flooring Tiles in Solid Teak Wood Oiled Finish (Set of 10), Long 9 Slat", | |
/// "totalQuantity": 0, | |
/// "type": "Amazon", | |
/// "typeId": 1 | |
/// }, | |
/// "image": "https://m.media-amazon.com/images/I/816a+lQ1w-L._AC_UL320_ML3_.jpg", | |
/// "link": "https://www.amazon.com/Bare-Decor-EZ-Floor-Interlocking-Flooring/dp/B014S7JPCO/ref=sr_1_3?keywords=Wood+floors&qid=1580015248&sr=8-3", | |
/// "price": "83.99", | |
/// "isUnopenedSuggestion": false, | |
/// "isMetattach": false, | |
/// "isMy": false, | |
/// "isSuggest": true, | |
/// "isWishlist": false, | |
/// "type": "Amazon" | |
/// } | |
/// ] | |
/// </remarks> | |
/// <param name="propertyId">The PropertyId</param> | |
/// <param name="profileItemId">The ProfileItemId</param> | |
/// <param name="lineItemId">The LineitemId</param> | |
/// <param name="lineItemTypeId">The LineitemTypeId</param> | |
/// <returns>The Amazon products from the Db</returns> | |
/// <response code="200">Returns the search results</response> | |
#endregion | |
[HttpGet] | |
[Route("api/amazon/{propertyId}/{profileItemId}/{lineItemId}/{lineItemTypeId}")] | |
[ProducesResponseType(200, Type = typeof(List<AmazonPayloadDto>))] | |
public async Task<ActionResult> GetAmazonAsync(long propertyId = 4, | |
long profileItemId = 0, | |
long lineItemId = 36, | |
long lineItemTypeId = 0) | |
{ | |
var result = await _amazonManager.GetAmazonAsync(propertyId, | |
profileItemId, | |
lineItemId, | |
lineItemTypeId).ConfigureAwait(false); | |
return Ok(result); | |
} | |
#region Swagger Documentation | |
/// <summary> | |
/// Get Amazon Products from the Db | |
/// </summary> | |
/// <remarks> | |
/// Sample request: | |
/// | |
/// GET api/amazon/proxy/2/0/84/0 | |
/// | |
/// Sample response: | |
/// | |
/// [ | |
/// { | |
/// "id": 27, | |
/// "name": "Headwest Reeded Sea Glass Wall Mirror, 24 inches by 30 inches, 24" x 30"", | |
/// "assetId": 86, | |
/// "assetInfo": { | |
/// "id": 86, | |
/// "price": 0, | |
/// "title": "Headwest Reeded Sea Glass Wall Mirror, 24 inches by 30 inches, 24" x 30"", | |
/// "totalQuantity": 0, | |
/// "type": "Amazon", | |
/// "typeId": 27 | |
/// }, | |
/// "image": "https://m.media-amazon.com/images/I/61DSpboAe1L._AC_UL320_ML3_.jpg", | |
/// "link": "https://www.amazon.com/Headwest-Reeded-Glass-Mirror-inches/dp/B00NOG2EZQ/ref=sr_1_8?keywords=Bathroom+mirrors&qid=1580926323&sr=8-8", | |
/// "price": "84.49", | |
/// "isUnopenedSuggestion": false, | |
/// "isMetattach": false, | |
/// "isMy": false, | |
/// "isSuggest": true, | |
/// "isWishlist": false, | |
/// "type": "Amazon" | |
/// } | |
/// ] | |
/// </remarks> | |
/// <param name="proxyPropertyId">The ProxyPropertyId</param> | |
/// <param name="profileItemId">The ProfileItemId</param> | |
/// <param name="lineItemId">The LineitemId</param> | |
/// <param name="lineItemTypeId">The LineitemTypeId</param> | |
/// <returns>The Amazon products from the Db</returns> | |
/// <response code="200">Returns the search results</response> | |
#endregion | |
[HttpGet] | |
[Route("api/amazon/proxy/{proxyPropertyId}/{profileItemId}/{lineItemId}/{lineItemTypeId}")] | |
[ProducesResponseType(200, Type = typeof(List<AmazonPayloadDto>))] | |
public async Task<ActionResult> GetAmazonProxyAsync(long proxyPropertyId = 2, | |
long profileItemId = 0, | |
long lineItemId = 84, | |
long lineItemTypeId = 0) | |
{ | |
var result = await _amazonManager.GetAmazonProxyAsync(proxyPropertyId, | |
profileItemId, | |
lineItemId, | |
lineItemTypeId).ConfigureAwait(false); | |
return Ok(result); | |
} | |
#region Swagger Documentation | |
/// <summary> | |
/// Save an Amazon product to the Db. | |
/// </summary> | |
/// <remarks> | |
/// Sample request: | |
/// | |
/// POST /api/amazon | |
/// // Saving an Amazon product for a lineitem which is also NOT a suggestion, should put message on SB Queue "AddItemToCategory" | |
/// // should be accessible for all ProfileItems which have this LineItem | |
/// { | |
/// "PropertyId": 4, | |
/// "LineItemId": 36, | |
/// "IsMy": true, | |
/// "ProductDetails": { | |
/// "Image": "some image url", | |
/// "Name": "My Amazon Product Name", | |
/// "Description": "This is the description of my Amazon product...", | |
/// "Link": "some link", | |
/// "Price": "9.99", | |
/// "Notes": "These are my personal notes about this thing..." | |
/// } | |
/// } | |
/// | |
/// POST /api/amazon | |
/// // TODO: Implement! | |
/// // Saving an Amazon product for a lineitem which is a suggestion, should put message on SB Queue "SuggestedItem" | |
/// { | |
/// "PropertyId": 4, | |
/// "LineItemId": 36, | |
/// "IsSuggest": true, | |
/// "ProductDetails": { | |
/// "Image": "some image url", | |
/// "Name": "My Amazon Product Name", | |
/// "Description": "This is the description of my Amazon product...", | |
/// "Link": "some link", | |
/// "Price": "9.99", | |
/// "Notes": "These are my personal notes about this thing..." | |
/// } | |
/// } | |
/// | |
/// POST /api/amazon | |
/// Saving an Amazon product for a ProfileItem and LineItem which is also NOT a suggestion, should put NOT message on SB Queue "AddItemToCategory" | |
/// should be accessible for the ProfileItem and LineItem to which is specified | |
/// { | |
/// "PropertyId": 4, | |
/// "ProfileItemId": 104, | |
/// "LineItemId": 77, | |
/// "IsMy": true, | |
/// "ProductDetails": { | |
/// "Image": "some image url", | |
/// "Name": "My Amazon Product Name", | |
/// "Description": "This is the description of my Amazon product...", | |
/// "Link": "some link", | |
/// "Price": "9.99", | |
/// "Notes": "These are my personal notes about this thing..." | |
/// } | |
/// } | |
/// | |
/// POST /api/amazon | |
/// // Saving an Amazon product for a ProfileItem and LineItem which is a suggestion, should put message on SB Queue "SuggestedItem" | |
/// // should be accessible for the ProfileItem and LineItem to which is specified | |
/// { | |
/// "PropertyId": 4, | |
/// "ProfileItemId": 104, | |
/// "LineItemId": 77, | |
/// "IsSuggest": true, | |
/// "ProductDetails": { | |
/// "Image": "some image url", | |
/// "Name": "My Amazon Product Name", | |
/// "Description": "This is the description of my Amazon product...", | |
/// "Link": "some link", | |
/// "Price": "9.99", | |
/// "Notes": "These are my personal notes about this thing..." | |
/// } | |
/// } | |
/// | |
/// </remarks> | |
/// <param name="searchResultDto"></param> | |
/// <response code="200">Ok()</response> | |
#endregion | |
[HttpPost] | |
[Route("api/amazon")] | |
[ProducesResponseType(200)] | |
public async Task<ActionResult> SaveAmazonAsync(SearchResultDto searchResultDto) | |
{ | |
var result = await _amazonManager.SaveAmazonAsync(searchResultDto).ConfigureAwait(false); | |
if (searchResultDto.ProfileItemId == 0) | |
{ | |
try | |
{ | |
_messagePublisher.Initialize(Configuration["ServiceBusConfig:Queues:AddItemToCategory"]); | |
var itemDto = new ItemDto | |
{ | |
EntityType = ArtifactType.Amazon.ToString(), | |
EntityId = result.Id, | |
PropertyId = searchResultDto.PropertyId, | |
ProxyPropertyId = searchResultDto.ProxyPropertyId, | |
ProfileItemId = searchResultDto.ProfileItemId, | |
LineItemId = searchResultDto.LineItemId, | |
IsSuggest = searchResultDto.IsSuggest | |
}; | |
await _messagePublisher.PublishMessage(JsonConvert.SerializeObject(itemDto)); | |
TelemetryClient.TrackEvent($"AddItemToCategory", new Dictionary<string, string> | |
{ | |
[Constants.ItemDto] = JsonConvert.SerializeObject(itemDto) | |
}); | |
} | |
catch (Exception ex) | |
{ | |
TelemetryClient.TrackException(ex); | |
throw; | |
} | |
finally | |
{ | |
_messagePublisher.Close(); | |
} | |
// TODO: Implement | |
//if (searchResultDto.IsSuggest) | |
//{ | |
// var propertyId = searchResultDto.PropertyId == null ? 0 : searchResultDto.PropertyId.Value; | |
// var proxyPropertyId = searchResultDto.ProxyPropertyId == null ? 0 : searchResultDto.ProxyPropertyId.Value; | |
// var a = await ItemManager.GetProfileItemIdsAsync(ArtifactType.Amazon, propertyId, proxyPropertyId, searchResultDto.ProfileItemId, searchResultDto.LineItemId).ConfigureAwait(false); | |
// a.ForEach( | |
// async (x) => | |
// { | |
// await Http.PostToRestClientAsync<ItemDto>($"{_functionUri}/api/suggested-item", new ItemDto | |
// { | |
// EntityType = "Amazon", | |
// EntityId = result.Id, | |
// ProfileItemId = x, | |
// LineItemId = searchResultDto.LineItemId | |
// }).ConfigureAwait(false); | |
// }); | |
//} | |
} | |
else | |
{ | |
if (searchResultDto.IsSuggest) | |
{ | |
try | |
{ | |
_messagePublisher.Initialize(Configuration["ServiceBusConfig:Queues:SuggestedItem"]); | |
var itemDto = new ItemDto | |
{ | |
EntityType = ArtifactType.Amazon.ToString(), | |
EntityId = result.Id, | |
ProfileItemId = searchResultDto.ProfileItemId, | |
LineItemId = searchResultDto.LineItemId | |
}; | |
await _messagePublisher.PublishMessage(JsonConvert.SerializeObject(itemDto)); | |
TelemetryClient.TrackEvent($"SuggestedItem", new Dictionary<string, string> | |
{ | |
[Constants.ItemDto] = JsonConvert.SerializeObject(itemDto) | |
}); | |
} | |
catch (Exception ex) | |
{ | |
TelemetryClient.TrackException(ex); | |
throw; | |
} | |
finally | |
{ | |
_messagePublisher.Close(); | |
} | |
} | |
} | |
return Ok(); | |
} | |
#region Swagger Documentation | |
/// <summary> | |
/// Update the AssetInfo for the Amazon product - (this does NOT update the actual data from Amazon but only the metadata about the item, but it will update the price) | |
/// </summary> | |
/// <remarks> | |
/// Sample request: | |
/// | |
/// PATCH /api/amazon | |
/// { | |
/// "Id": 1, | |
/// "Name": "Test Update", | |
/// "Price": 9.99 | |
/// } | |
/// | |
/// </remarks> | |
/// <param name="amazonPayloadDto">The properties to update</param> | |
/// <response code="200">Ok()</response> | |
#endregion | |
[HttpPatch] | |
[Route("api/amazon")] | |
[ProducesResponseType(200)] | |
public async Task<ActionResult> UpdateAmazonAsync(AmazonPayloadDto amazonPayloadDto) | |
{ | |
await _amazonManager.UpdateAsync(amazonPayloadDto).ConfigureAwait(false); | |
return Ok(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment