Esempio n. 1
0
        public async Task <string> UpdateCustomer(Logic.Customer customer)
        {
            Customer e_customer = await dbcontext.Customer.FindAsync(customer.CustomerID);

            if (e_customer == null)
            {
                return("no such customer");
            }

            if (customer.LastName != null)
            {
                e_customer.LastName = customer.LastName;
            }
            if (customer.FirstName != null)
            {
                e_customer.FirstName = customer.FirstName;
            }
            if (customer.Email != null)
            {
                e_customer.Email = customer.Email;
            }
            if (customer.Password != null)
            {
                e_customer.Password = customer.Password;
            }

            await dbcontext.SaveChangesAsync();

            //logger.Info();

            return("update success");
        }
        public async Task <IActionResult> Delete(int id)
        {
            Logic.Customer cus = new Logic.Customer();
            cus.CustomerID = id;

            await iRepo.DeleteCustomer(cus);

            return(Ok());
        }
Esempio n. 3
0
        public async Task <string> CreateCustomer(Logic.Customer customer)
        {
            Customer e_customer = Mapper.MapCustomerToE(customer);

            dbcontext.Add(e_customer);
            await dbcontext.SaveChangesAsync();

            //logger.Info();

            return($"{customer.FirstName} {customer.LastName} is created.");
        }
Esempio n. 4
0
        public async Task <List <Logic.Customer> > ReadCustomerList(Logic.Customer customer)
        {
            if (customer == null)
            {
                List <Customer> customerList = await dbcontext.Customer.ToListAsync();

                return(customerList.Select(Mapper.MapEToCustomer).ToList());
            }

            if (customer.CustomerID <= 0)
            {
                IQueryable <Customer> q_cusotmer = dbcontext.Customer;

                if (customer.FirstName != null)
                {
                    q_cusotmer = q_cusotmer.Where(c => c.FirstName == customer.FirstName)
                                 .AsNoTracking();
                }
                if (customer.LastName != null)
                {
                    q_cusotmer = q_cusotmer.Where(c => c.LastName == customer.LastName)
                                 .AsNoTracking();
                }
                if (customer.Email != null)
                {
                    q_cusotmer = q_cusotmer.Where(c => c.Email == customer.Email)
                                 .AsNoTracking();
                }
                if (customer.Password != null)
                {
                    q_cusotmer = q_cusotmer.Where(c => c.Password == customer.Password)
                                 .AsNoTracking();
                }

                List <Customer> customerFind = await q_cusotmer.ToListAsync();

                List <Logic.Customer> resultCustomer = customerFind.Select(Mapper.MapEToCustomer).ToList();
                if (resultCustomer.ToList().Count < 1)
                {
                    //logger.Warn();
                    return(null);
                }
                return(resultCustomer);
            }
            else
            {
                List <Logic.Customer> customerFind = new List <Logic.Customer>();
                customerFind.Add(Mapper.MapEToCustomer(await dbcontext.Customer.FindAsync(customer.CustomerID)));

                //logger.Info();
                return(customerFind);
            }
        }
        //[Authorize]
        public async Task <ActionResult> Create([FromBody, Bind("FirstName, LastName, Email, Password")] API.Models.APICustomer customer)
        {
            Logic.Customer cus = new Logic.Customer
            {
                CustomerID = customer.CustomerID,
                FirstName  = customer.FirstName,
                LastName   = customer.LastName,
                Email      = customer.Email,
                Password   = customer.Password
            };

            await iRepo.CreateCustomer(cus);

            return(CreatedAtRoute("GetCustomer", new { id = cus.CustomerID }, cus));
        }
Esempio n. 6
0
        public async Task <string> DeleteCustomer(Logic.Customer customer)
        {
            Customer e_customer = await dbcontext.Customer.FindAsync(customer.CustomerID);

            if (e_customer == null)
            {
                //logger.Warn("customer not found")
                return("no such customer");
            }

            dbcontext.Remove(dbcontext.Customer.Find(customer.CustomerID));
            dbcontext.SaveChanges();

            //logger.info();
            return("delete success");
        }
        public void MapEToCustomerTest()
        {
            d.Customer customer1 = new d.Customer
            {
                CustomerID = 1,
                FirstName  = "tri",
                LastName   = "nguyen",
                Email      = "123@321",
                Password   = "******"
            };

            l.Customer customer2 = d.Mapper.MapEToCustomer(customer1);

            Assert.Equal(customer1.CustomerID, customer2.CustomerID);
            Assert.Equal(customer1.FirstName, customer2.FirstName);
            Assert.Equal(customer1.LastName, customer2.LastName);
            Assert.Equal(customer1.Email, customer2.Email);
            Assert.Equal(customer1.Password, customer2.Password);
        }
        public async Task <IActionResult> Put([FromBody] API.Models.APICustomer Acustomer)
        {
            Logic.Customer cus = new Logic.Customer();

            IEnumerable <Logic.Customer> Lcustomers = await iRepo.ReadCustomerList(cus);

            //Remember to add try catch or some exception handling
            Logic.Customer newCus = new Logic.Customer
            {
                CustomerID = Acustomer.CustomerID,
                FirstName  = Acustomer.FirstName,
                LastName   = Acustomer.LastName,
                Email      = Acustomer.Email,
                Password   = Acustomer.Password
            };

            await iRepo.UpdateCustomer(newCus);

            return(Ok());
        }
        //[ApiKeyAuth]
        //[Authorize]
        public async Task <IEnumerable <API.Models.APICustomer> > GetAllCustomers(int id)
        {
            int maxId = await iRepo.GetCustomerId();

            if (id <= 0 || id > maxId)
            {
                IEnumerable <Logic.Customer> customers = await iRepo.ReadCustomerList(null);

                IEnumerable <API.Models.APICustomer> apiCustomer = customers.Select(c => new API.Models.APICustomer
                {
                    //From APIModel = Logic
                    CustomerID = c.CustomerID,
                    FirstName  = c.FirstName,
                    LastName   = c.LastName,
                    Email      = c.Email,
                    Password   = c.Password
                });

                return(apiCustomer);
            }
            else
            {
                Logic.Customer Lcus = new Logic.Customer();
                Lcus.CustomerID = id;
                IEnumerable <Logic.Customer> customers = await iRepo.ReadCustomerList(Lcus);

                IEnumerable <API.Models.APICustomer> apiCustomer = customers.Select(c => new API.Models.APICustomer
                {
                    //From APIModel = Logic
                    CustomerID = c.CustomerID,
                    FirstName  = c.FirstName,
                    LastName   = c.LastName,
                    Email      = c.Email,
                    Password   = c.Password
                });

                return(apiCustomer);
            }
        }