Esempio n. 1
0
        public string Retrieve(string userId, string firstName, DateTime weightDate)
        {
            try
            {
                using (IDynamoDBContext context = Factory.DynamoDBContext)
                {
                    WeighInUser user = context.LoadAsync <WeighInUser>(userId, firstName).Result;

                    if (user == null)
                    {
                        throw new WeighInException($"User \"{userId},{firstName}\" not found");
                    }

                    WeighInWeight weight = context.LoadAsync <WeighInWeight>(user.UserKey, weightDate).Result;

                    return(JsonConvert.SerializeObject(weight));
                }
            }
            catch (Exception ex)
            {
                Factory.Logger.Log($"Error getting WeighInWeight with userId=\"{userId}\" and firstName=\"{firstName}\" and weightDate=\"{weightDate}\"");
                Factory.Logger.Log(ex.Message);
                Factory.Logger.Log(ex.StackTrace);
                throw new WeighInException(ex.Message);
            }
        }
Esempio n. 2
0
        public void TestRetrieve_Success()
        {
            string   actualUserKey    = string.Empty;
            DateTime?actualWeightDate = null;

            Mock <IDynamoDBContext> context = new Mock <IDynamoDBContext>();

            context.Setup(D => D.LoadAsync <WeighInWeight>(It.IsAny <string>(), It.IsAny <DateTime?>(), It.IsAny <CancellationToken>())).Returns((string a, DateTime b, object c) =>
            {
                actualUserKey        = a;
                actualWeightDate     = b;
                WeighInWeight weight = new WeighInWeight()
                {
                    UserKey    = a,
                    WeightDate = b,
                    Weight     = 88.8m
                };

                return(Task.FromResult <WeighInWeight>(weight));
            });
            context.Setup(D => D.LoadAsync <WeighInUser>(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns((string a, string b, object c) =>
            {
                WeighInUser user = new WeighInUser()
                {
                    UserId             = a,
                    FirstName          = b,
                    UserKey            = "UnitTestKey",
                    StartingWeight     = 88.8m,
                    StartingWeightDate = new DateTime(2018, 7, 16)
                };
                return(Task.FromResult <WeighInUser>(user));
            });

            TestAwsFactory factory = new TestAwsFactory()
            {
                DynamoDBContext = context.Object
            };

            using (GetWeight getWeight = new GetWeight(factory))
            {
                string jsonResult = getWeight.Retrieve("UnitTestId", "Unit", new DateTime(2018, 7, 16));

                Console.WriteLine(jsonResult);

                Assert.That(actualUserKey, Is.EqualTo("UnitTestKey"), "actualUserKey");
                Assert.That(actualWeightDate, Is.EqualTo(new DateTime(2018, 7, 16)), "actualWeightDate");

                dynamic user = JObject.Parse(jsonResult);
                Assert.That((string)user.UserKey, Is.EqualTo("UnitTestKey"), "UserKey");
                Assert.That((DateTime)user.WeightDate, Is.EqualTo(new DateTime(2018, 7, 16)), "WeightDate");
                Assert.That((decimal)user.Weight, Is.EqualTo(88.8m), "Weight");
            }
        }
Esempio n. 3
0
        public void TestSave()
        {
            WeighInWeight actualWeight = null;

            Mock <IDynamoDBContext> context = new Mock <IDynamoDBContext>();

            context.Setup(C => C.SaveAsync <WeighInWeight>(It.IsAny <WeighInWeight>(), It.IsAny <CancellationToken>())).Returns((WeighInWeight weight, object t) =>
            {
                actualWeight = weight;
                return(Task.CompletedTask);
            });
            context.Setup(D => D.LoadAsync <WeighInUser>(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns((string a, string b, object c) =>
            {
                WeighInUser user = new WeighInUser()
                {
                    UserId             = a,
                    FirstName          = b,
                    UserKey            = "UnitTestKey",
                    StartingWeight     = 88.8m,
                    StartingWeightDate = new DateTime(2018, 7, 16)
                };
                return(Task.FromResult <WeighInUser>(user));
            });

            TestAwsFactory factory = new TestAwsFactory()
            {
                DynamoDBContext = context.Object
            };

            using (PostWeight postWeight = new PostWeight(factory))
            {
                string weightOutputJson = postWeight.Save("UnitTestId", "Unit", DateTime.Today, 78.8m);

                Assert.That(actualWeight, Is.Not.Null, "actualUser");
                Assert.That(actualWeight.UserKey, Is.Not.Null.And.Not.Empty, "actualUserKey");
                Assert.That(actualWeight.Weight, Is.EqualTo(78.8m), "actualWeight");
                Assert.That(actualWeight.WeightDate, Is.EqualTo(DateTime.Today), "actualWeightDate");


                dynamic weight = JObject.Parse(weightOutputJson);
                Assert.That((string)weight.UserKey, Is.Not.Null.And.Not.Empty, "UserKey");
                Assert.That((decimal)weight.Weight, Is.EqualTo(78.8m), "StartingWeight");
                Assert.That((DateTime)weight.WeightDate, Is.EqualTo(DateTime.Today), "StartingWeightDate");
            }
        }
Esempio n. 4
0
        public string Save(string userId, string firstName, DateTime weightDate, decimal weight)
        {
            try
            {
                Factory.Logger.Log($"Using User Id: {userId}");
                Factory.Logger.Log($"Using Weight Date: {weightDate}");
                Factory.Logger.Log($"Using Weight: {weight}");

                using (IDynamoDBContext context = Factory.DynamoDBContext)
                {
                    WeighInUser user = context.LoadAsync <WeighInUser>(userId, firstName).Result;

                    if (user == null)
                    {
                        throw new WeighInException($"User \"{userId},{firstName}\" not found");
                    }

                    Factory.Logger.Log($"Saving weight for \"{user.UserKey}\"");
                    WeighInWeight weighInWeight = new WeighInWeight()
                    {
                        UserKey    = user.UserKey,
                        WeightDate = weightDate,
                        Weight     = weight
                    };
                    Task t = context.SaveAsync <WeighInWeight>(weighInWeight);
                    t.Wait();
                    if (t.Status == TaskStatus.Faulted)
                    {
                        throw t.Exception;
                    }
                    Factory.Logger.Log($"Weight saved for \"{user.UserKey}\"");

                    return(JsonConvert.SerializeObject(weighInWeight));
                }
            }
            catch (Exception ex)
            {
                Factory.Logger.Log($"Error saving WeighInWeight with userId=\"{userId}\" and firstName=\"{firstName}\" and weightDate=\"{weightDate}\"");
                Factory.Logger.Log(ex.Message);
                Factory.Logger.Log(ex.StackTrace);
                throw new WeighInException(ex.Message);
            }
        }