Example #1
0
        private APIGatewayProxyResponse HandleUpdateAsync(APIGatewayProxyRequest pRequest, ILambdaContext pContext, CognitoUser pCognitoUser, opendkpContext pDatabase)
        {
            var vResponse = HttpHelper.HandleError("[InsertOrUpdateAdjustment] 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);
                Adjustments vAdjustment = new Adjustments()
                {
                    Name         = vModel.Name,
                    Description  = vModel.Description,
                    Value        = vModel.Value,
                    Timestamp    = vModel.Timestamp,
                    IdAdjustment = vModel.Id,
                    ClientId     = vClientId
                };
                int vId = vModel.Id;
                if (vAdjustment != null)
                {
                    Adjustments vExists = pDatabase.Adjustments
                                          .FirstOrDefault(x => x.ClientId.Equals(vClientId) && x.IdAdjustment == vId);
                    string vOld = JsonConvert.SerializeObject(vExists, new JsonSerializerSettings {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });

                    vAdjustment.IdCharacter = vExists.IdCharacter;
                    pDatabase.Entry(vExists).CurrentValues.SetValues(vAdjustment);
                    pDatabase.SaveChanges();
                    vResponse = HttpHelper.HandleResponse(vModel, 200);

                    //Audit
                    AuditHelper.InsertAudit(pDatabase, vClientId, vOld, pRequest.Body, pCognitoUser.Username, Audit.ACTION_ADJUST_UPDATE);

                    //Update Caches
                    int vStatus = CacheManager.UpdateSummaryCacheAsync(vClientId).GetAwaiter().GetResult();
                    Console.WriteLine("StatusCode for CacheUpdate=" + vStatus);
                }
            }
            catch (Exception vException)
            {
                Console.WriteLine("1 Exception: " + vException);
                vResponse = HttpHelper.HandleError(vException.Message, 500);
            }
            return(vResponse);
        }
Example #2
0
        private APIGatewayProxyResponse HandleUpdate(APIGatewayProxyRequest pRequest, ILambdaContext pContext, CognitoUser pCognitoUser, opendkpContext pDatabase, string pClientId)
        {
            var vResponse = HttpHelper.HandleError("[Update] Unknown error backend...", 500);

            try
            {
                Characters vModel = JsonConvert.DeserializeObject <Characters>(pRequest.Body);
                if (vModel != null)
                {
                    Characters vExists = pDatabase.Characters.FirstOrDefault(x => x.ClientId.Equals(pClientId) &&
                                                                             x.IdCharacter == vModel.IdCharacter);
                    if (vExists != null && vExists.IdCharacter >= 0)
                    {
                        string vOld = JsonConvert.SerializeObject(vExists,
                                                                  new JsonSerializerSettings {
                            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                        });

                        //CustomCharacter class is a subset model of Characters, design to update only specific properties
                        CharacterModel vSimplifiedModel = new CharacterModel(vModel);
                        pDatabase.Entry(vExists).CurrentValues.SetValues(vSimplifiedModel);
                        pDatabase.SaveChanges();
                        AuditHelper.InsertAudit(pDatabase, pClientId, vOld, pRequest.Body, pCognitoUser.Username, Audit.ACTION_CHAR_UPDATE);
                        vResponse = HttpHelper.HandleResponse(vSimplifiedModel, 200);
                    }
                    else
                    {
                        vResponse = HttpHelper.HandleError("Character not found in database", 500);
                    }
                }
            }
            catch (Exception vException)
            {
                vResponse = HttpHelper.HandleError(vException.Message, 500);
            }
            return(vResponse);
        }