public PeopleController(
     ILogger <PeopleController> logger,
     PersonDbContext context)
 {
     _context = context;
     _logger  = logger;
 }
        public async Task PostPerson()
        {
            // Arrange
            var options = new DbContextOptionsBuilder <PersonDbContext>()
                          .UseInMemoryDatabase(databaseName: "TPSDatabase")
                          .Options;

            using (var context = new PersonDbContext(options, true))
            {
                var peopleController = new PeopleController(new NullLogger <PeopleController>(), context);

                // Act
                var result = await peopleController.PostPerson(new Person
                {
                    FirstName  = "Sam",
                    LastName   = "Doe",
                    Address    = "123 Main St",
                    City       = "Anytown",
                    State      = "AA",
                    PostalCode = "11111",
                    ImageName  = "profile.png",
                    ImageData  = null,
                });

                // Assert
                Assert.IsNotNull(context.Persons.Where(b => b.FirstName == "Sam").FirstOrDefault());
            }

            await Task.CompletedTask;
        }
        public Person GetPerson(int personId)
        {
            using var db = new PersonDbContext();
            var person = db.Persons.FirstOrDefault(x => x.Id == personId);

            return(person);
        }
Esempio n. 4
0
 static void Main(string[] args)
 {
     try
     {
         ManageDatabase();
         using (var ctx = new PersonDbContext())
         {
             var Person = new Person()
             {
                 PersonName = "Abhijeet", Gender = "Male", Age = 28
             };
             ctx.Persons.Add(Person);
             ctx.SaveChanges();
             Console.WriteLine("Added Person");
             foreach (var p in ctx.Persons.ToList())
             {
                 //$ use as interpolation no need to do  + p.Personid+p.PersoName
                 Console.WriteLine($"{p.PersonId}{p.PersonName}{p.Age}{p.Gender}");
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine($"{ex.Message}{ex.InnerException}");
     }
     Console.ReadLine();
 }
Esempio n. 5
0
        public Dictionary <string, Person> PullPeopleDictionary()
        {
            Dictionary <string, Person> MyPersonDictionary = new Dictionary <string, Person>();

            using (var context = new PersonDbContext())
            {
                List <People> MyPeople = context.People
                                         .ToList();
                List <Locations> MyLocations = context.Locations
                                               .ToList();

                foreach (var val in MyPeople)
                {
                    Person MyPerson = new Person();
                    MyPerson.SetPassword(val.Password);
                    MyPerson.SetName(val.Username);
                    MyPerson.SetEmployeeTag(val.Employee);
                    var MyLocationQuary = context.Locations
                                          .First(Location => Location.LocationId == val.LocationId).LocationName;

                    MyPerson.SetLocation(MyLocationQuary);

                    MyPersonDictionary.Add(MyPerson.GetName(), MyPerson);
                }
                return(MyPersonDictionary);
            }
        }
 public PersonRepository(PersonDbContext dbContext, ILogger <PersonRepository> logger, IMapper mapper)
 {
     this.dbContext = dbContext;
     this.logger    = logger;
     this.mapper    = mapper;
     SeedData();
 }
Esempio n. 7
0
        public bool CreateLocation(string MyNewLocation)
        {
            using (PersonDbContext context = new PersonDbContext())
            {
                var DataLocations = context.Locations;

                var ExistsQuary = DataLocations.FirstOrDefault(p => p.LocationName == MyNewLocation);

                if (ExistsQuary == null)
                {
                    Locations MyNewLocationData = new Locations()
                    {
                        LocationId   = DataLocations.Max(p => p.LocationId) + 1,
                        LocationName = MyNewLocation
                    };
                    DataLocations.Add(MyNewLocationData);
                    try
                    {
                        context.SaveChanges();
                        return(true);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
                return(false);
            }
        }
        public List <Pet> GetAllPets(int personId)
        {
            using var db = new PersonDbContext();
            var personpets = db.Pets.Where(x => x.Id == personId).ToList();

            return(personpets);
        }
Esempio n. 9
0
 static void Main(string[] args)
 {
     try
     {
         ManageDatabse();
         using (var ctx = new PersonDbContext())
         {
             var person = new Person()
             {
                 PersonName = "Sonam",
                 Age        = 43,
                 Gender     = "Female"
             };
             ctx.Persons.Add(person);
             ctx.SaveChanges();
             Console.WriteLine("Added Person");
             foreach (var p in ctx.Persons.ToList())
             {
                 Console.WriteLine($"{p.PersonId} {p.PersonName} {p.Age} {p.Gender}");
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
     Console.ReadLine();
 }
Esempio n. 10
0
        static void Main(string[] args)
        {
            try
            {
                ManageDatabse();

                using (var ctx = new PersonDbContext())
                {
                    var person = new Person()
                    {
                        //PersonId = 1,    this will get created as identity column in db so not required
                        PersonName = "Ketan",
                        Age        = 32,
                        Gender     = "Male"
                    };

                    ctx.Persons.Add(person);
                    ctx.SaveChanges();
                    Console.WriteLine("Added Person");

                    foreach (var p in ctx.Persons.ToList())
                    {
                        Console.WriteLine($"{p.PersonId} {p.PersonName} {p.Age} {p.Gender}");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex.Message} {ex.InnerException}");
            }

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            try
            {
                MonitorDb();

                var per = new Person()
                {
                    PersonId   = 1001,
                    PersonName = "Mahesh Sabnis",
                    Address    = "Bavdhan"
                };

                // set values for the private members by calling the public methods
                per.SetContatcNo(992325);
                per.SetIncome(120000);

                using (var ctx = new PersonDbContext())
                {
                    ctx.Persons.Add(per);
                    ctx.SaveChanges();

                    // read the data
                    foreach (var p in ctx.Persons.ToList())
                    {
                        Console.WriteLine($"{p.PersonId} {p.PersonName} {p.Address} {p.GetContactNo()} {p.GetIncome()}");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error Occured {ex.Message}");
            }
            Console.ReadLine();
        }
Esempio n. 12
0
        public static void Initialize(PersonDbContext context)
        {
            if (context.People.Any())
            {
                return;
            }

            var people = new List <PersonRecord>
            {
                new PersonRecord {
                    FirstName = "Bill", LastName = "Gates", DateOfBirth = new DateTime(1955, 10, 28), Street = "Microsoft way", City = "Seattle", State = "Washington", PostalCode = "98134", PictureName = "a4759609-0051-4cd5-a84b-1d4efa743c97.jpg"
                },
                new PersonRecord {
                    FirstName = "Mark", LastName = "Zuckerberg", DateOfBirth = new DateTime(1984, 5, 14), Street = "Facebook street", City = "White Plains", State = "New York", PostalCode = "10016", PictureName = "4dc720fc-cb7b-4f94-a004-21101c1a64e7.jpg"
                },
                new PersonRecord {
                    FirstName = "Jeff", LastName = "Bezos", DateOfBirth = new DateTime(1964, 1, 12), Street = "Amazon Prime way", City = "Albuquerque", State = "New Mexico", PostalCode = "87110", PictureName = "f1c1d561-a45d-4d8c-bb37-4641de13a64e.jpg"
                },
                new PersonRecord {
                    FirstName = "Steve", LastName = "Jobs", DateOfBirth = new DateTime(1955, 02, 24), Street = "Apple Parkway", City = "San Francisco", State = "California", PostalCode = "90201", PictureName = "bc3b154e-135c-4fd3-aee0-e9dfceee8286.jpg"
                },
                new PersonRecord {
                    FirstName = "Elon", LastName = "Musk", DateOfBirth = new DateTime(1971, 6, 28), Street = "Tesla street way", City = "Pretoria", State = "South Africa", PostalCode = "Unknown", PictureName = "2702f7df-bce7-4f3b-8741-efe32215435b.jpg"
                },
                new PersonRecord {
                    FirstName = "Tim", LastName = "Cook", DateOfBirth = new DateTime(1960, 11, 1), Street = "Apple Parkway", City = "Mobile", State = "Alabama", PostalCode = "36611", PictureName = "3271aeb1-2db7-470f-acac-b375a9002cc4.jpg"
                },
            };

            people.ForEach(p => context.People.Add(p));
            context.SaveChangesAsync();
        }
Esempio n. 13
0
        public List <House> GetHousesByPostal(int postal)
        {
            using var db = new PersonDbContext();
            List <House> listOpPostal = db.Houses.Where(x => x.Postal == postal).ToList();

            return(listOpPostal);
        }
Esempio n. 14
0
        static void Main(string[] args)
        {
            Producer producer = new Producer();
            Consumer consumer = new Consumer();



            Console.WriteLine("Enter person name");
            string username = Console.ReadLine();

            using (var db = new PersonDbContext())
            {
                var person = new Person
                {
                    Name = username
                };

                db.persons.Add(person);
                db.SaveChanges();
                producer.produceMesaage(person);
            }



            consumer.consumeMessages();
            Console.ReadLine();
        }
 // helper method to Create a database if it is not created. (Note: You can also delete the already created database)
 static void MonitorDb()
 {
     using (var context = new PersonDbContext())
     {
         context.Database.EnsureDeleted(); // Whole DB Delete
         context.Database.EnsureCreated();
     }
 }
Esempio n. 16
0
 // this method make sure if DB is already available then delete it
 static void ManageDatabse()
 {
     using (var ctx = new PersonDbContext())
     {
         ctx.Database.EnsureDeleted();
         ctx.Database.EnsureCreated();
     }
 }
        public void DeletePersonById(int personId)
        {
            using var db = new PersonDbContext();
            var personToDelete = db.Persons.Find(personId);

            db.Persons.Remove(personToDelete);
            db.SaveChanges();
        }
Esempio n. 18
0
 public void CreatePerson(Person person)
 {
     using (var db = new PersonDbContext())
     {
         db.Persons.Add(person);
         db.SaveChanges();
     }
 }
Esempio n. 19
0
        public List <Pet> GetPetByType(PetTypes Type)
        {
            using var db = new PersonDbContext();

            List <Pet> petsByType = db.Pets.Where(type => type.PetType == Type).ToList();

            return(petsByType);
        }
Esempio n. 20
0
 public List <Pet> GetPets()
 {
     using (var db = new PersonDbContext())
     {
         var listOfPets = db.Pets.ToList();
         return(listOfPets);
     }
 }
Esempio n. 21
0
 public Pet GetPet(int Id)
 {
     using (var db = new PersonDbContext())
     {
         var pet = db.Pets.FirstOrDefault(x => x.Id == Id);
         return(pet);
     }
 }
        public Person ChangePassword(int personIdPasswordChange, Person password)
        {
            using var db = new PersonDbContext();
            var newPassword = db.Persons.First(person => person.Id == personIdPasswordChange);

            newPassword.Password = password.Password;
            db.SaveChanges();
            return(newPassword);
        }
Esempio n. 23
0
 // レコードの削除
 static void Delete(int id)
 {
     using (var db = new PersonDbContext())
     {
         var person = db.Persons.Where(x => x.Id == id).FirstOrDefault();
         db.Persons.Remove(person);
         db.SaveChanges();
     }
 }
Esempio n. 24
0
        public Dictionary <string, Store> PullStoreDictionary()
        {
            Dictionary <string, Store> MyStoreDictionary = new Dictionary <string, Store>();

            using (var context = new PersonDbContext())
            {
                List <GeneralStock> MyGeneralStock = context.GeneralStock
                                                     .Include(p => p.StoreStock)
                                                     .Include(p => p.StoreStock.Location)
                                                     .Include(p => p.Topic)
                                                     .ToList();
                List <Locations> MyLocations = context.Locations
                                               .ToList();
                foreach (var val1 in MyLocations)
                {
                    List <Stock> MyCurrentStockList = new List <Stock>();
                    foreach (var val2 in MyGeneralStock)
                    {
                        if (val2.StoreStock == null)
                        {
                            continue;
                        }
                        if (val2.StoreStock.LocationId == val1.LocationId)
                        {
                            Stock MyCurrentStock = new Stock();
                            MyCurrentStock.SetDescription(val2.StockDescription);
                            MyCurrentStock.SetName(val2.StockName);
                            MyCurrentStock.SetPrice(Convert.ToDouble(val2.Price));
                            MyCurrentStock.SetTopic(val2.Topic.TopicName);
                            MyCurrentStockList.Add(MyCurrentStock);
                        }
                    }

                    if (MyStoreDictionary.ContainsKey(val1.LocationName))
                    {
                        foreach (var val in MyCurrentStockList)
                        {
                            MyStoreDictionary[val1.LocationName].AddStock(val);
                        }
                    }
                    else
                    {
                        Store MyNewStore = new Store();
                        MyNewStore.SetName(val1.LocationName);
                        Dictionary <string, List <Stock> > SpecificStoreStock = new Dictionary <string, List <Stock> >();
                        MyNewStore.SetMyStock(SpecificStoreStock);
                        MyStoreDictionary.Add(val1.LocationName, MyNewStore);
                        foreach (var val in MyCurrentStockList)
                        {
                            MyStoreDictionary[val1.LocationName].AddStock(val);
                        }
                    }
                }
            }

            return(MyStoreDictionary);
        }
Esempio n. 25
0
        internal RepositoryBase(PersonDbContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            _context = context;
        }
Esempio n. 26
0
 // レコードの更新
 static void Update(int id, string name)
 {
     using (var db = new PersonDbContext())
     {
         var person = db.Persons.Where(x => x.Id == id).FirstOrDefault();
         person.Name = name;
         db.SaveChanges();
     }
 }
Esempio n. 27
0
        public PersonRepository(PersonDbContext context)
        {
            if (context.People.Count() == 0)
            {
                context.Initialize();
                context.SaveChanges();
            }

            this.context = context;
        }
        public Person Login(string email, string password)
        {
            using var db = new PersonDbContext();
            var login = db.Persons.FirstOrDefault(person => person.Email == email);

            login.Email = email;
            //login.Password = password;
            db.SaveChanges();
            return(login);
        }
Esempio n. 29
0
 public void Init()
 {
     using (var db = new PersonDbContext())
     {
         var persons = repository.GetPersons();
         db.Persons.AddRange(persons);
         db.SaveChanges();
         Console.WriteLine("Data saved");
     }
 }
Esempio n. 30
0
 public House CreateHouse(House house)
 {
     using (var db = new PersonDbContext())
     {
         // Create
         db.Houses.Add(house);
         db.SaveChanges();
         return(house);
     }
 }