public void TestCustomBatchPropValues()
        {
            // Prerequisite entities
            Plastic         pp   = PlasticsRepository.CreatePlastic("PP", "Polypropylene");
            Material        mat  = MaterialsRepository.CreateMaterial("mat", "manu", "manu-id", pp);
            StorageSite     site = LocationsRepository.CreateStorageSite("test_site");
            StorageArea     area = LocationsRepository.CreateStorageArea(site, "test_area");
            StorageLocation loc  = new StorageLocation()
            {
                StorageSiteId   = site.Id,
                StorageAreaId   = area.Id,
                StorageSiteName = site.Name,
                StorageAreaName = area.Name
            };
            CustomBatchProp prop1 = PropRepository.CreateCustomBatchProp("prop-1");
            CustomBatchProp prop2 = PropRepository.CreateCustomBatchProp("prop-2");

            // Create batch with custom prop values
            MaterialBatch batch = Repository.CreateMaterialBatch(mat, DateTime.Today.AddDays(17), loc, 42, 42, new Dictionary <Guid, string>()
            {
                { prop1.Id, "Ak Bars" },
                { prop2.Id, "Aloha" }
            }, false);

            Assert.Equal(2, batch.CustomProps.Count);

            // Test updating
            batch.CustomProps[prop1.Id] = "UPDATE TEST";
            Repository.UpdateMaterialBatch(batch);
            batch = Repository.GetMaterialBatch(batch.Id);
            Assert.Equal(2, batch.CustomProps.Count);
            Assert.Single(batch.CustomProps.Where(p => p.Value == "UPDATE TEST"));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates a plastic.
        /// </summary>
        /// <param name="id">The ID of the plastic to update.</param>
        /// <param name="name">The name of the plastic to update.</param>
        /// <returns>Returns the updated plastic.</returns>
        /// <exception cref="PlasticNotFoundException">Thrown if no matching material plastic could not be found.</exception>
        public Plastic UpdatePlastic(string id, string name)
        {
            Plastic plastic = GetPlasticOrThrowNotFoundException(id);

            plastic.Name = name;
            PlasticsRepository.UpdatePlastic(plastic);
            return(plastic);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates a new plastic.
 /// </summary>
 /// <param name="id">The ID of the new plastic.</param>
 /// <param name="name">The name of the plastic to create.</param>
 /// <returns>Returns the newly created plastic.</returns>
 public Plastic CreatePlastic(string id, string name)
 {
     if (IsPlasticsIdTaken(id))
     {
         throw new PlasticAlreadyExistsException(id);
     }
     return(PlasticsRepository.CreatePlastic(id, name));
 }
Exemplo n.º 4
0
        /// <summary>
        /// Attempts to get a plastic from the underlying repository and throws a <see cref="PlasticNotFoundException"/> if no matching plastic could be found.
        /// </summary>
        /// <param name="id">ID of the plastic to get.</param>
        /// <exception cref="PlasticNotFoundException">Thrown if no matching plastic could be found.</exception>
        /// <returns>Returns the plastic, if found.</returns>
        private Plastic GetPlasticOrThrowNotFoundException(string id)
        {
            Plastic plastic = PlasticsRepository.GetPlastic(id);

            if (plastic == null)
            {
                throw new PlasticNotFoundException($"Could not find any plastic with Id `${id}`.");
            }
            return(plastic);
        }
Exemplo n.º 5
0
        public void TestTransactionRepository()
        {
            // Create prerequisite entities
            Plastic         plastic  = PlasticsRepository.CreatePlastic("PP", "Polypropylene");
            Material        material = MaterialsRepository.CreateMaterial("m", "m", "m", plastic);
            StorageSite     site     = LocationsRepository.CreateStorageSite("site");
            StorageArea     area     = LocationsRepository.CreateStorageArea(site, "area");
            StorageLocation location = new StorageLocation()
            {
                StorageSiteId   = site.Id,
                StorageAreaId   = area.Id,
                StorageSiteName = site.Name,
                StorageAreaName = area.Name
            };
            MaterialBatch batch = BatchRepository.CreateMaterialBatch(material, DateTime.Today.AddDays(3), location, 42, 42, new Dictionary <Guid, string>(), false);

            (string password, byte[] salt) = new PasswordHashingService(new LoggerFactory()).HashAndSaltPassword("test");
            User user = UserRepository.CreateUser("alex", "Alex", password, salt, Role.Administrator);

            // Create transactions
            Repository.LogTransaction(new Transaction()
            {
                Id = Guid.NewGuid(),
                MaterialBatchId = batch.Id,
                Quantity        = -3,
                Timestamp       = DateTime.Today.AddDays(-3),
                UserId          = user.Id
            });
            Repository.LogTransaction(new Transaction()
            {
                Id = Guid.NewGuid(),
                MaterialBatchId = batch.Id,
                Quantity        = -2,
                Timestamp       = DateTime.Today.AddDays(-2),
                UserId          = user.Id
            });
            Guid lastId = Guid.NewGuid();

            Repository.LogTransaction(new Transaction()
            {
                Id = lastId,
                MaterialBatchId = batch.Id,
                Quantity        = -1,
                Timestamp       = DateTime.Today.AddDays(-1),
                UserId          = user.Id
            });

            // Check with getAll
            IEnumerable <Transaction> transactions = Repository.GetTransactionsForBatch(batch.Id);

            Assert.Equal(3, transactions.Count());
            Assert.Single(transactions, t => t.Quantity == -3);
            Assert.Single(transactions, t => t.Quantity == -2);
            Assert.Single(transactions, t => t.Quantity == -1);

            // Check get last
            Transaction transaction = Repository.GetLastTransactionForBatch(batch.Id);

            Assert.Equal(batch.Id, transaction.MaterialBatchId);
            Assert.Equal(lastId, transaction.Id);
            Assert.Equal(-1, transaction.Quantity);

            // Test transaction update
            transaction.Quantity = 200;
            Repository.UpdateTransaction(transaction);
            transaction = Repository.GetLastTransactionForBatch(batch.Id);
            Assert.Equal(lastId, transaction.Id);
            Assert.Equal(200, transaction.Quantity);
        }
        public void TestMaterialBatchRepository()
        {
            Assert.Empty(Repository.GetAllMaterialBatches());

            // Create prerequisite entities
            Plastic         pp   = PlasticsRepository.CreatePlastic("PP", "Polypropylene");
            Plastic         ep   = PlasticsRepository.CreatePlastic("EP", "Epoxy");
            Material        pp1  = MaterialsRepository.CreateMaterial("pp-1", "Umbrella Corp.", "pp-1", pp);
            Material        ep1  = MaterialsRepository.CreateMaterial("ep-1", "Umbrella Corp.", "ep-1", ep);
            StorageSite     site = LocationsRepository.CreateStorageSite("Test Site");
            StorageArea     area = LocationsRepository.CreateStorageArea(site, "Test Area");
            StorageLocation loc  = new StorageLocation()
            {
                StorageSiteId   = site.Id,
                StorageAreaId   = area.Id,
                StorageSiteName = site.Name,
                StorageAreaName = area.Name
            };

            // Create batch and check return
            DateTime      expDate = DateTime.Today.AddDays(5);
            MaterialBatch batch   = Repository.CreateMaterialBatch(pp1, expDate, loc, 1, 125.0, new Dictionary <Guid, string>(), false);

            Assert.Equal(pp1.Id, batch.Material.Id);
            Assert.Equal(expDate, batch.ExpirationDate);
            Assert.Equal(area.Id, batch.StorageLocation.StorageAreaId);
            Assert.Equal(site.Id, batch.StorageLocation.StorageSiteId);
            Assert.Equal(1, batch.BatchNumber);
            Assert.Equal(125.0, batch.Quantity);
            Assert.Empty(batch.CustomProps);
            Assert.False(batch.IsLocked);
            Assert.False(batch.IsArchived);

            // Check get all batches
            IEnumerable <MaterialBatch> batches = Repository.GetAllMaterialBatches();

            Assert.Single(batches);
            batch = batches.Single();
            Assert.Equal(pp1.Id, batch.Material.Id);
            Assert.Equal(expDate, batch.ExpirationDate);
            Assert.Equal(area.Id, batch.StorageLocation.StorageAreaId);
            Assert.Equal(site.Id, batch.StorageLocation.StorageSiteId);
            Assert.Equal(1, batch.BatchNumber);
            Assert.Equal(125.0, batch.Quantity);
            Assert.Empty(batch.CustomProps);
            Assert.False(batch.IsLocked);
            Assert.False(batch.IsArchived);

            // Check get single batch
            batch = Repository.GetMaterialBatch(batch.Id);
            Assert.Equal(pp1.Id, batch.Material.Id);
            Assert.Equal(expDate, batch.ExpirationDate);
            Assert.Equal(area.Id, batch.StorageLocation.StorageAreaId);
            Assert.Equal(site.Id, batch.StorageLocation.StorageSiteId);
            Assert.Equal(1, batch.BatchNumber);
            Assert.Equal(125.0, batch.Quantity);
            Assert.Empty(batch.CustomProps);
            Assert.False(batch.IsLocked);
            Assert.False(batch.IsArchived);

            // Check filters
            batches = Repository.GetMaterialBatches(pp1.Id);
            Assert.Single(batches);
            batch = batches.Single();
            Assert.Equal(pp1.Id, batch.Material.Id);
            batches = Repository.GetMaterialBatches(siteId: site.Id);
            Assert.Single(batches);
            batch = batches.Single();
            Assert.Equal(pp1.Id, batch.Material.Id);
            batches = Repository.GetMaterialBatches(pp1.Id, site.Id);
            Assert.Single(batches);
            batch = batches.Single();
            Assert.Equal(pp1.Id, batch.Material.Id);
            batches = Repository.GetMaterialBatches(99999, site.Id);
            Assert.Empty(batches);
            batches = Repository.GetMaterialBatches(pp1.Id, area.Id);
            Assert.Empty(batches);

            // Create another batch and test filters again
            Repository.CreateMaterialBatch(ep1, expDate, loc, 33, 25.5, new Dictionary <Guid, string>(), false);
            batches = Repository.GetMaterialBatches(ep1.Id, site.Id);
            Assert.Single(batches);
            batch = batches.Single();
            Assert.Equal(ep1.Id, batch.Material.Id);
            batches = Repository.GetMaterialBatches(siteId: site.Id);
            Assert.Equal(2, batches.Count());
            Assert.Single(batches, b => b.Material.Id == pp1.Id);
            Assert.Single(batches, b => b.Material.Id == ep1.Id);

            // Test updating a batch
            batch = batches.Single(b => b.Material.Id == ep1.Id);
            Assert.Equal(33, batch.BatchNumber);
            Assert.Equal(25.5, batch.Quantity);
            Assert.False(batch.IsLocked);
            batch.BatchNumber = 333;
            batch.Quantity    = 203.1;
            batch.IsLocked    = true;
            Repository.UpdateMaterialBatch(batch);
            batch = Repository.GetMaterialBatch(batch.Id);
            Assert.Equal(333, batch.BatchNumber);
            Assert.Equal(203.1, batch.Quantity);
            Assert.True(batch.IsLocked);

            // Test archiving
            Guid archivedId = batch.Id;

            batch.IsArchived = true;
            Repository.UpdateMaterialBatch(batch);
            batches = Repository.GetAllMaterialBatches();
            Assert.Single(batches);
            batch = batches.Single();
            Assert.Equal(pp1.Id, batch.Material.Id);

            // Still directly gettable by ID
            batch = Repository.GetMaterialBatch(archivedId);
            Assert.Equal(ep1.Id, batch.Material.Id);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Gets and filters plastics using a partial name.
 /// </summary>
 /// <param name="partialName">String to filter with..</param>
 /// <returns>Returns filtered plastics.</returns>
 public IEnumerable <Plastic> GetPlastics(string partialName)
 {
     return(PlasticsRepository.SearchPlasticsByName(partialName));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Gets all available plastics.
 /// </summary>
 /// <returns>Returns all plastics.</returns>
 public IEnumerable <Plastic> GetPlastics()
 {
     return(PlasticsRepository.GetAllPlastics());
 }
Exemplo n.º 9
0
        /// <summary>
        /// Determines whether a plastics ID is taken.
        /// </summary>
        /// <param name="id">The ID to check.</param>
        /// <returns>Returns whether the ID is taken.</returns>
        private bool IsPlasticsIdTaken(string id)
        {
            Plastic plastic = PlasticsRepository.GetPlastic(id);

            return(plastic != null);
        }