Exemplo n.º 1
0
        public static IEnumerable <AccountTransaction> ReadInputFile(string path, SaveItUser saveitUser, Account account, Category category, IList <Person> people)
        {
            foreach (string line in File.ReadAllLines(path))
            {
                string[] parts = line.Split(',');
                if (!parts.All(p => string.IsNullOrWhiteSpace(p)))
                {
                    string             concept            = parts[2];
                    decimal            total              = decimal.Parse(parts[3]);
                    AccountTransaction accountTransaction = new AccountTransaction
                    {
                        SaveItUserId    = saveitUser.Id,
                        SaveItUser      = saveitUser,
                        Date            = DateTime.Parse(parts[0]),
                        BankDescription = parts[1],
                        AccountId       = account.Id,
                        Account         = account,
                        Transactions    = new List <Transaction>()
                    };

                    for (int i = 4; i < parts.Length; i++)
                    {
                        string  partValue = parts[i];
                        Person  person    = people[i - 4];
                        decimal amount;
                        if (!string.IsNullOrWhiteSpace(partValue) && decimal.TryParse(partValue, out amount))
                        {
                            Transaction transaction = new Transaction
                            {
                                SaveItUserId         = saveitUser.Id,
                                SaveItUser           = saveitUser,
                                Amount               = amount,
                                Concept              = concept,
                                PersonId             = person.Id,
                                Person               = person,
                                CategoryId           = category.Id,
                                Category             = category,
                                AccountTransactionId = accountTransaction.Id,
                            };

                            accountTransaction.Transactions.Add(transaction);
                        }
                    }

                    decimal calculatedTotal = accountTransaction.Transactions.Sum(t => t.Amount);

                    if (total != calculatedTotal)
                    {
                        throw new Exception("Data is invalid");
                    }

                    yield return(accountTransaction);
                }
            }
        }
        public async Task UpdateAsync(SaveItUser user)
        {
            var existingUser = await this.GetAsync(user.Id);

            if (existingUser == null)
            {
                throw new InvalidOperationException(
                          string.Format("The user with id {0} does not exist", user.Id));
            }

            existingUser.Name = user.Name;
        }
Exemplo n.º 3
0
            public async Task DeletesUser()
            {
                var existingId   = new Guid("57a04ab4-b614-4d83-bf38-8063791c63f2");
                var existingUser = new SaveItUser {
                    Id = existingId, Name = "existingUser"
                };

                context.Users.Add(existingUser);

                await repository.DeleteAsync(new Guid("57a04ab4-b614-4d83-bf38-8063791c63f2"));

                context.Users.Should().BeEmpty();
            }
Exemplo n.º 4
0
        public static IEnumerable <SaveItUser> ReadUsers(string path)
        {
            foreach (string line in File.ReadAllLines(path))
            {
                string[] parts = line.Split('\t');

                if (parts[0].Equals(typeof(SaveItUser).ToString()))
                {
                    SaveItUser user = JsonConvert.DeserializeObject <SaveItUser>(parts[1]);
                    yield return(user);
                }
            }
        }
Exemplo n.º 5
0
            public async Task CreatesUser()
            {
                var user = new SaveItUser {
                    Name = "testUser"
                };

                await repository.CreateAsync(user);

                context.Users.Should().NotBeEmpty();
                context.Users.Count.Should().Be(1);
                context.Users[0].Id.Should().NotBe(Guid.Empty);
                user.Id.Should().NotBe(Guid.Empty);
                context.Users[0].Id.Should().Be(user.Id);
            }
Exemplo n.º 6
0
            public async Task RespectsUserId()
            {
                var id   = new Guid("57a04ab4-b614-4d83-bf38-8063791c63f2");
                var user = new SaveItUser {
                    Id = id, Name = "testUser"
                };

                await repository.CreateAsync(user);

                context.Users.Should().NotBeEmpty();
                context.Users.Count.Should().Be(1);
                context.Users[0].Id.Should().Be(id);
                user.Id.Should().Be(id);
                context.Users[0].Id.Should().Be(user.Id);
            }
Exemplo n.º 7
0
            public void ThrowsIfUserNameExists()
            {
                var existingUser = new SaveItUser {
                    Name = "existingUser"
                };
                var user = new SaveItUser {
                    Name = "existingUser"
                };

                context.Users.Add(existingUser);

                Func <Task> act = async() => await repository.CreateAsync(user);

                act.ShouldThrow <InvalidOperationException>()
                .Where(e => e.Message.Contains("existingUser"));
            }
Exemplo n.º 8
0
            public void ThrowsIfUserIdExists()
            {
                var existingId   = new Guid("57a04ab4-b614-4d83-bf38-8063791c63f2");
                var existingUser = new SaveItUser {
                    Id = existingId, Name = "existingUser"
                };
                var id   = new Guid("57a04ab4-b614-4d83-bf38-8063791c63f2");
                var user = new SaveItUser {
                    Id = id, Name = "testUser"
                };

                context.Users.Add(existingUser);

                Func <Task> act = async() => await repository.CreateAsync(user);

                act.ShouldThrow <InvalidOperationException>()
                .Where(e => e.Message.Contains("57a04ab4-b614-4d83-bf38-8063791c63f2"));
            }
Exemplo n.º 9
0
        public static void CreateEntityFile(string[] args)
        {
            Guid       userId = new Guid("1333ca66-0252-444a-aae1-64bd1ca5013a");
            SaveItUser user   = new SaveItUser {
                Id = userId, Name = "coseguera"
            };

            string[] accounts = { "BOA", "FTS", "FTC" };
            string[] people   = { "Comida", "Carlos", "Liz", "Niños", "Servicios", "Inversion", "Proyectos", "Deudas", "Apoyo", "Educacion", "Vacaciones", "Medico", "Casa", "Seguros", "Muebles" };

            string       filePath = GetFilePath(entityFileRelativePath);
            SaveItWriter writer   = new SaveItWriter(filePath);

            writer.WriteUserAsync(user).Wait();

            Account account;

            foreach (string name in accounts)
            {
                account = new Account {
                    Name = name, SaveItUserId = userId
                };
                writer.WriteEntityAsync(account).Wait();
            }

            Category category = new Category {
                Name = "Unfiled", SaveItUserId = userId
            };

            writer.WriteEntityAsync(category).Wait();

            Person person;

            foreach (string name in people)
            {
                person = new Person {
                    Name = name, SaveItUserId = userId
                };
                writer.WriteEntityAsync(person).Wait();
            }

            writer.Dispose();
        }
Exemplo n.º 10
0
            public async Task UpdatesUser()
            {
                var existingId   = new Guid("57a04ab4-b614-4d83-bf38-8063791c63f2");
                var existingUser = new SaveItUser {
                    Id = existingId, Name = "existingUser"
                };

                context.Users.Add(existingUser);

                var user = new SaveItUser {
                    Id = existingId, Name = "newName"
                };

                await repository.UpdateAsync(user);

                context.Users.Count.Should().Be(1);
                context.Users[0].Id.Should().Equals(new Guid("57a04ab4-b614-4d83-bf38-8063791c63f2"));
                context.Users[0].Name.Should().Equals("newName");
            }
        public async Task CreateAsync(SaveItUser user)
        {
            SaveItUser existingUser = await this.GetByNameAsync(user.Name);

            if (existingUser != null)
            {
                throw new InvalidOperationException(
                          string.Format("The user with name {0} already exists", user.Name));
            }

            existingUser = await this.GetAsync(user.Id);

            if (existingUser != null)
            {
                throw new InvalidOperationException(
                          string.Format("The user with id {0} already exists", user.Id));
            }

            this.db.Users.Add(user);
        }
Exemplo n.º 12
0
 public async Task WriteUserAsync(SaveItUser user)
 {
     string json = JsonConvert.SerializeObject(user);
     await writer.WriteLineAsync(
         string.Format("{0}\t{1}", typeof(SaveItUser), json));
 }