Exemplo n.º 1
0
        public Category Add(Category category)
        {
            var existingCategory = _dbContext.Categories
                                   .SingleOrDefault(x => x.Name.Equals(category.Name, StringComparison.InvariantCultureIgnoreCase));

            if (existingCategory != null)
            {
                return(existingCategory);
            }

            _dbContext.Categories.Add(category);
            _dbContext.SaveChanges();
            return(category);
        }
Exemplo n.º 2
0
 public Institution Add(Institution institution)
 {
     _dbContext.Institutions.Add(institution);
     _dbContext.SaveChanges();
     return(institution);
 }
Exemplo n.º 3
0
        private void Seed()
        {
            var institutions = new List <Institution>();

            institutions.Add(new Institution
            {
                ID          = 1,
                Name        = "Institution1",
                Description = "An examplar institution1"
            });
            institutions.Add(new Institution
            {
                ID          = 2,
                Name        = "Institution2",
                Description = "An examplar institution2"
            });

            var donations = new List <Donation>();

            donations.Add(new Donation
            {
                ID                = 1,
                Quantity          = 5,
                Street            = "TestStreet 1",
                City              = "TestCity1",
                ZipCode           = "TestZipCode1",
                PickUpDateAndTime = new DateTime(2019, 12, 17, 14, 15, 0),
                PickUpComment     = "A random comment"
            });
            donations.Add(new Donation
            {
                ID                = 2,
                Quantity          = 15,
                Street            = "TestStreet 2",
                City              = "TestCity2",
                ZipCode           = "TestZipCode2",
                PickUpDateAndTime = new DateTime(2017, 10, 5, 12, 0, 0),
                PickUpComment     = null
            });
            donations.Add(new Donation
            {
                ID                = 3,
                Quantity          = 8,
                Street            = "TestStreet 3",
                City              = "TestCity3",
                ZipCode           = "TestZipCode3",
                PickUpDateAndTime = new DateTime(2019, 1, 29, 8, 40, 0),
                PickUpComment     = "A random comment"
            });

            var categories = new List <Category>();

            categories.Add(new Category
            {
                ID   = 1,
                Name = "Category1"
            });
            categories.Add(new Category
            {
                ID   = 2,
                Name = "Category2"
            });
            categories.Add(new Category
            {
                ID   = 3,
                Name = "Category3"
            });
            categories.Add(new Category
            {
                ID   = 4,
                Name = "Category4"
            });
            categories.Add(new Category
            {
                ID   = 5,
                Name = "Category5"
            });

            var relations = new List <CategoriesDonationsRelation>();

            relations.Add(new CategoriesDonationsRelation
            {
                ID = 1,
                //Donation = donation[0],
                DonationID = donations[0].ID,
                //Category = categories[0],
                CategoryID = categories[0].ID
            });
            relations.Add(new CategoriesDonationsRelation
            {
                ID = 2,
                //Donation = donation[0],
                DonationID = donations[0].ID,
                //Category = categories[1],
                CategoryID = categories[1].ID
            });
            relations.Add(new CategoriesDonationsRelation
            {
                ID = 3,
                //Donation = donation[0],
                DonationID = donations[0].ID,
                //Category = categories[2],
                CategoryID = categories[2].ID
            });

            relations.Add(new CategoriesDonationsRelation
            {
                ID = 4,
                //Donation = donation[1],
                DonationID = donations[1].ID,
                //Category = categories[1],
                CategoryID = categories[1].ID
            });
            relations.Add(new CategoriesDonationsRelation
            {
                ID = 5,
                //Donation = donation[1],
                DonationID = donations[1].ID,
                //Category = categories[2],
                CategoryID = categories[2].ID
            });
            relations.Add(new CategoriesDonationsRelation
            {
                ID = 6,
                //Donation = donation[1],
                DonationID = donations[1].ID,
                //Category = categories[3],
                CategoryID = categories[3].ID
            });

            relations.Add(new CategoriesDonationsRelation
            {
                ID = 7,
                //Donation = donation[2],
                DonationID = donations[2].ID,
                //Category = categories[3],
                CategoryID = categories[3].ID
            });
            relations.Add(new CategoriesDonationsRelation
            {
                ID = 8,
                //Donation = donation[2],
                DonationID = donations[2].ID,
                //Category = categories[4],
                CategoryID = categories[4].ID
            });

            using var context = new DonationContext();
            context.Donations.AddRange(donations);
            context.Categories.AddRange(categories);
            context.Institutions.AddRange(institutions);
            context.CategoryDonationRelations.AddRange(relations);
            context.SaveChanges();
        }
Exemplo n.º 4
0
        public Donation Add(Donation donation, IEnumerable <Category> categories)
        {
            using var transaction = _dbContext.Database.BeginTransaction();

            try
            {
                _dbContext.Donations.Add(donation);
                _dbContext.SaveChanges();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }

            if (donation.DonationCategories is null)
            {
                donation.DonationCategories = new List <CategoriesDonationsRelation>();
            }

            foreach (var item in categories)
            {
                Category category = null;

                try
                {
                    category = CreateCategory(category);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }

                if (category.DonationCategories is null)
                {
                    category.DonationCategories = new List <CategoriesDonationsRelation>();
                }

                CategoriesDonationsRelation relation = null;

                try
                {
                    relation = CreateRelation(donation, category);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }

                try
                {
                    AddRelationToDonationAndCategory(donation, category, relation);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }

            transaction.Commit();

            return(donation);
        }
 public CategoriesDonationsRelation Add(CategoriesDonationsRelation relation)
 {
     _dbContext.CategoryDonationRelations.Add(relation);
     _dbContext.SaveChanges();
     return(relation);
 }