/// <summary> /// Lambda to insert character to character table /// </summary> /// <param name="pRequest">Incoming API Gateway request object, should be a PUT or POST with a BODY</param> /// <param name="pContext">Incoming Lambda Context object, not used currently</param> /// <returns></returns> public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest pRequest, ILambdaContext pContext) { if (pRequest.Headers.Keys.Contains("warmup")) { return(HttpHelper.WarmupResponse()); } var vResponse = HttpHelper.HandleError("[DeleteRaid] Unknown Backend error", 500); try { if (pRequest != null && pRequest.PathParameters != null && pRequest.PathParameters.Count > 0) { int vId = int.Parse(pRequest.PathParameters["id"]); using (opendkpContext vDatabase = new opendkpContext()) { //Authorized Users only for Deleting Raids if (!CognitoHelper.IsDkpAdmin(pRequest) || !CognitoHelper.IsAuthorizedAdmin(vDatabase, pRequest)) { return(HttpHelper.HandleError("You do not have permission to perform this action", 401)); } var vCognitoUser = CognitoHelper.GetCognitoUser(pRequest.Headers["cognitoinfo"]); //We need to retrieve the ClientId for multitenancy purposes var vClientId = pRequest.Headers["clientid"]; Raids vResult = RaidHelper.DeleteRaid(vDatabase, vId, vClientId); vDatabase.SaveChanges(); vResponse = HttpHelper.HandleResponse(vResult, 200); //Audit AuditHelper.InsertAudit(vDatabase, vClientId, vResult, string.Empty, vCognitoUser.Username, Audit.ACTION_RAID_DELETE); //Update Caches int vStatus = CacheManager.UpdateSummaryCacheAsync(vClientId).GetAwaiter().GetResult(); Console.WriteLine("SummaryCacheResponse=" + vStatus); vStatus = CacheManager.UpdateItemCacheAsync(vClientId).GetAwaiter().GetResult(); Console.WriteLine("ItemCacheResponse=" + vStatus); } } } catch { vResponse = HttpHelper.HandleError("[DeleteRaid] Issue with DB or Query", 500); } return(vResponse); }
private APIGatewayProxyResponse HandleInsert(APIGatewayProxyRequest pRequest, ILambdaContext pContext, CognitoUser pCognitoUser, opendkpContext pDatabase) { var vResponse = HttpHelper.HandleError("[InsertOrUpdateRaid] Unknown error backend...", 500); //We need to retrieve the ClientId for multitenancy purposes var vClientId = pRequest.Headers["clientid"]; try { //Populate Model dynamic vModel = JsonConvert.DeserializeObject(pRequest.Body); using (var dbContextTransaction = pDatabase.Database.BeginTransaction()) { Dictionary <string, Characters> vCharacterModels = RaidHelper.GetCharacterModels(pDatabase, vClientId, vModel); vModel.IdRaid = RaidHelper.CreateRaidId(pDatabase, vClientId, vModel); RaidHelper.InsertRaidItems(pDatabase, vClientId, vModel, vCharacterModels); RaidHelper.InsertRaidTicks(pDatabase, vClientId, vModel, vCharacterModels); pDatabase.SaveChanges(); dbContextTransaction.Commit(); vResponse = HttpHelper.HandleResponse(vModel, 200); AuditHelper.InsertAudit(pDatabase, vClientId, string.Empty, vModel, pCognitoUser.Username, Audit.ACTION_RAID_INSERT); //Update Caches int vStatus = CacheManager.UpdateSummaryCacheAsync(vClientId).GetAwaiter().GetResult(); Console.WriteLine("SummaryCacheResponse=" + vStatus); vStatus = CacheManager.UpdateItemCacheAsync(vClientId).GetAwaiter().GetResult(); Console.WriteLine("ItemCacheResponse=" + vStatus); } } catch (Exception vException) { vResponse = HttpHelper.HandleError(vException.Message, 500); } return(vResponse); }
private APIGatewayProxyResponse HandleUpdate(APIGatewayProxyRequest pRequest, ILambdaContext pContext, CognitoUser pCognitoUser, opendkpContext pDatabase) { var vResponse = HttpHelper.HandleError("[InsertOrUpdateAdjustment] Unknown error backend...", 500); try { //Populate Model dynamic vModel = JsonConvert.DeserializeObject(pRequest.Body); int vIdRaid = vModel.IdRaid; //We need to retrieve the ClientId for multitenancy purposes var vClientId = pRequest.Headers["clientid"]; Dictionary <string, Characters> vCharacterModels = RaidHelper.GetCharacterModels(pDatabase, vClientId, vModel); using (var dbContextTransaction = pDatabase.Database.BeginTransaction()) { Raids vRaid = pDatabase.Raids. Include("Ticks.TicksXCharacters"). Include("ItemsXCharacters.Item"). Include("ItemsXCharacters.Character"). Include("IdPoolNavigation"). FirstOrDefault(x => x.ClientId.Equals(vClientId) && x.IdRaid == vIdRaid); dynamic vOldModel = GetAuditModel(vRaid); //Update some attributes of the raid vRaid.Name = vModel.Name; vRaid.Timestamp = vModel.Timestamp; vRaid.UpdatedTimestamp = DateTime.Now; vRaid.UpdatedBy = vModel.UpdatedBy; vRaid.IdPool = vModel.Pool.IdPool; vRaid.ClientId = vClientId; //Three Cases to handle: Raid Tick Added, Raid Tick Removed, Raid Tick Updated #region Handle Raid Tick Removed here SimpleTick[] vSimpleTicks = vModel.Ticks.ToObject <SimpleTick[]>(); List <Ticks> vRemoveTicks = new List <Ticks>(); foreach (Ticks vIndex in vRaid.Ticks) { var vFound = vSimpleTicks.FirstOrDefault(x => x.TickId == vIndex.TickId); if (vFound == null) { vRemoveTicks.Add(vIndex); } } foreach (Ticks vTick in vRemoveTicks) { vRaid.Ticks.Remove(vTick); } #endregion #region Handle Raid Tick Added & Raid Tick Updated Here foreach (var vTick in vModel.Ticks) { int?vTickId = vTick.TickId; //If tickId is null, we have to create an insert a new one if (vTickId == null) { RaidHelper.CreateTick(pDatabase, vClientId, vRaid.IdRaid, vTick, vCharacterModels); } else { Ticks vFoundTick = vRaid.Ticks.FirstOrDefault(x => x.TickId == vTickId); if (vFoundTick != null) { vFoundTick.Description = vTick.Description; vFoundTick.Value = vTick.Value; vFoundTick.ClientId = vClientId; //Now that I've found the tick vFoundTick.TicksXCharacters = new List <TicksXCharacters>(); foreach (string vAttendee in vTick.Attendees) { vFoundTick.TicksXCharacters.Add(new TicksXCharacters { IdCharacterNavigation = vCharacterModels[vAttendee.ToLower()], IdTickNavigation = vFoundTick, ClientId = vClientId }); } } else { throw new Exception(string.Format("The tick id {0} does not exist, will not save raid", vTickId)); } } } #endregion //Handle Items vRaid.ItemsXCharacters = new List <ItemsXCharacters>(); RaidHelper.InsertRaidItems(pDatabase, vClientId, vModel, vCharacterModels); //Save pDatabase.SaveChanges(); dbContextTransaction.Commit(); //Respond vResponse = HttpHelper.HandleResponse(vModel, 200); //Audit AuditHelper.InsertAudit(pDatabase, vClientId, vOldModel, vModel, pCognitoUser.Username, Audit.ACTION_RAID_UPDATE); //Update Caches int vStatus = CacheManager.UpdateSummaryCacheAsync(vClientId).GetAwaiter().GetResult(); Console.WriteLine("SummaryCacheResponse=" + vStatus); vStatus = CacheManager.UpdateItemCacheAsync(vClientId).GetAwaiter().GetResult(); Console.WriteLine("ItemCacheResponse=" + vStatus); } } catch (Exception vException) { vResponse = HttpHelper.HandleError(vException.Message, 500); } return(vResponse); }