示例#1
0
        public bool UpdatePerson(PersonModel personUpdate, string email)
        {
            // TODO: update admin/customer -id
            using (var db = new TankshopDbContext())
            {
                try
                {
                    var editPerson = db.People.Find(email);
                    var editPersonModel = GetPerson(email);

                    editPerson.Firstname = personUpdate.Firstname;
                    editPerson.Lastname = personUpdate.Lastname;
                    editPerson.Address = personUpdate.Address;

                    var personPostal = db.Postals.Find(personUpdate.Zipcode);
                    if (personPostal == null)
                    {
                        var oldPostal = db.Postals.Find(editPerson.Zipcode);
                        if (oldPostal != null)
                            oldPostal.People.Remove(editPerson);
                        db.SaveChanges();

                        personPostal = new Postal()
                        {
                            Zipcode = personUpdate.Zipcode,
                            City = personUpdate.City
                        };
                        personPostal.People.Add(editPerson);
                        db.SaveChanges();
                    }

                    editPerson.Zipcode = personUpdate.Zipcode;
                    editPerson.Postal = personPostal;

                    db.SaveChanges();

                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
示例#2
0
        public bool AddPerson(PersonModel person, Role role, string password)
        {
            var email = person.Email;
            var newPerson = new Person()
            {
                Email = email,
                Firstname = person.Firstname,
                Lastname = person.Lastname,
                Address = person.Address,
                Zipcode = person.Zipcode,

            };
            using (var db = new TankshopDbContext())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        var personPostal = db.Postals.Find(person.Zipcode);
                        if (personPostal == null)
                        {
                            personPostal = new Postal()
                            {
                                Zipcode = person.Zipcode,
                                City = person.City
                            };
                        }
                        personPostal.People.Add(newPerson);
                        newPerson.Postal = personPostal;

                        // Create email / password - combination
                        var existingCredentials = db.Credentials.Find(email);
                        if (existingCredentials != null)
                            return false;

                        var passwordHash = CreateHash(password);
                        var newCredentials = new Credential()
                        {
                            Email = email,
                            Password = passwordHash
                        };
                        db.Credentials.Add(newCredentials);

                        // Set Customer / AdminId
                        int AdminId = 0, CustomerId = 0;
                        if (role == Role.Admin)
                        {
                            var dbAdmin = db.Admins.FirstOrDefault(a => a.Email == email);
                            if (dbAdmin == null)
                            {
                                dbAdmin = new Admin()
                                {
                                    Email = email
                                };
                                db.Admins.Add(dbAdmin);
                            }
                            AdminId = dbAdmin.AdminId;
                        }
                        if (role == Role.Customer)
                        {
                            var dbCustomer = db.Customers.FirstOrDefault(c => c.Email == email);
                            if (dbCustomer == null)
                            {
                                dbCustomer = new Nettbutikk.Model.Customer()
                                {
                                    Email = email
                                };
                                db.Customers.Add(dbCustomer);
                            }
                            CustomerId = dbCustomer.CustomerId;

                        }

                        db.People.Add(newPerson);

                        db.SaveChanges();
                        transaction.Commit();

                        return true;

                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        return false;
                    }
                }
            }
        }