using System.Collections.Generic; using System.Linq; using Nancy; using Nancy.ModelBinding; using Sony.Filtr.Contracts.Abstractions; using Sony.Filtr.Globalization.v3; namespace Sony.Filtr.AdminAPI.Modules.Globalization { public class MarketGlobalizationModule : BaseModule { private const int DefaultApiConsumerId = 1; public MarketGlobalizationModule(MarketGlobalizationHandler globalizationHandler, IApplicationInstanceManager applicationInstanceManager) : base(applicationInstanceManager) { Get["GetMarketGlobalizationKeys", "/marketglobalization/Keys"] = parameters => { var keys = globalizationHandler.GetKeys(); return Response.AsJson(keys); }; Post["AddMarketGlobalizationKey", "/marketglobalization/Keys/"] = parameters => { var keyObject = this.Bind(); if (keyObject.ApiConsumerId == default(int)) keyObject.ApiConsumerId = DefaultApiConsumerId; globalizationHandler.SetKey(keyObject); return HttpStatusCode.Accepted; }; Delete["DeleteMarketGlobalizationKey", "/marketglobalization/Keys/"] = parameters => { var key = Request.Query.key; var context = Request.Query.context; var apiConsumerId = Request.Query.apiConsumerId; if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(context)) return HttpStatusCode.BadRequest; if (apiConsumerId == null || apiConsumerId == default(int)) apiConsumerId = DefaultApiConsumerId; globalizationHandler.DeleteKey(key, context, apiConsumerId); return HttpStatusCode.Accepted; }; Get["GetMarketGlobalizationValues", "/marketglobalization/{countryCode}/Values"] = parameters => { if(CurrentApplication == null) return HttpStatusCode.BadRequest; var globalization = globalizationHandler.GetTranslations(CurrentApplication.ID); var returnValues = globalization.Select( kvp => new GlobalizationResult() { Key = kvp.Key.Key, Context = kvp.Key.Context, Description = kvp.Key.Description, DefaultValue = kvp.Key.DefaultValue, Value = kvp.Value != null ? kvp.Value.Value : null, CountryCode = CurrentApplication.SpotifyRegionCode, ApiConsumerId = kvp.Key.ApiConsumerId, }); return Response.AsJson(returnValues); }; Post["AddMarketGlobalizationValue", "/marketglobalization/{countryCode}/Value"] = parameters => { var globalizationUpdate = this.Bind(); if (CurrentApplication == null || globalizationUpdate == null || globalizationUpdate.Key == null || globalizationUpdate.Context == null) return HttpStatusCode.BadRequest; if (globalizationUpdate.ApiConsumerId == default(int)) globalizationUpdate.ApiConsumerId = DefaultApiConsumerId; globalizationHandler.SetTranslation(globalizationUpdate.Key, globalizationUpdate.Context, globalizationUpdate.ApiConsumerId, CurrentApplication.ID, globalizationUpdate.Value); return HttpStatusCode.Accepted; }; Post["AddMarketGlobalizationValues", "/marketglobalization/{countryCode}/Values"] = parameters => { var globalizationUpdates = this.Bind(); if (CurrentApplication == null || globalizationUpdates == null || globalizationUpdates.Values == null) return HttpStatusCode.BadRequest; foreach (var update in globalizationUpdates.Values) { if (update.ApiConsumerId == default(int)) update.ApiConsumerId = DefaultApiConsumerId; if (update.Value != null) { globalizationHandler.SetTranslation(update.Key, update.Context, update.ApiConsumerId, CurrentApplication.ID, update.Value); } else { globalizationHandler.DeleteValue(update.Key, update.Context, update.ApiConsumerId, CurrentApplication.ID); } } return HttpStatusCode.Accepted; }; Delete["DeleteMarketGlobalizationValues", "/marketglobalization/{countryCode}/Values"] = parameters => { var key = Request.Query.key; var context = Request.Query.context; var apiConsumerId = Request.Query.apiConsumerId; if (CurrentApplication == null || string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(context)) return HttpStatusCode.BadRequest; if (apiConsumerId == null || apiConsumerId == default(int)) apiConsumerId = DefaultApiConsumerId; globalizationHandler.DeleteValue(key, context, apiConsumerId, CurrentApplication.ID); return HttpStatusCode.Accepted; }; } public class GlobalizationResult { public string Key { get; set; } public string Context { get; set; } public string DefaultValue { get; set; } public string Description { get; set; } public string Value { get; set; } public string CountryCode { get; set; } public int ApiConsumerId { get; set; } } public class GlobalizationUpdate { public string Key { get; set; } public string Context { get; set; } public int ApiConsumerId { get; set; } public string Value { get; set; } } public class GlobalizationUpdates { public List Values { get; set; } } } }