<repo_name>StaffanJ/PizzaChallenge<file_sep>/Pizza.Domain/CustomerCheckValidationException.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pizza.Domain
{
    public class CustomerCheckValidationException : Exception
    {
        public string Message;

        public CustomerCheckValidationException(string Message)
        {
            this.Message = Message;
        }
    }
}
<file_sep>/Pizza.Presentation/Default.aspx.cs
﻿using System;
using Pizza.Domain;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Data.Entity.Validation;

namespace Pizza.Presentation
{
    public partial class Default : System.Web.UI.Page
    {
        PizzaManager pizzaManager = new PizzaManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                displayInformation();
                displayCustomers();
            }
            resultLabel.Text = gatherInformation();
        }
        private void displayCustomers()
        {
            var customers = pizzaManager.GetCustomers();
            foreach (var customer in customers)
            {
                customerDropDown.Items.Add(new ListItem(customer.Name, customer.customerID.ToString()));
            }

        }
        private void displayInformation()
        {
            var crusts = pizzaManager.GetCrusts();
            var ingredients = pizzaManager.GetIngredients();
            var sizes = pizzaManager.GetSizes();

            foreach (var crust in crusts)
            {
                crustDropDownList.Items.Add(new ListItem(crust.Name, crust.Price.ToString()));
            }
            foreach (var size in sizes)
            {
                sizesDropDownList.Items.Add(new ListItem(size.Name, size.Price.ToString()));
            }
            foreach (var ingredient in ingredients)
            {
                ingredientsCheckBox.Items.Add(new ListItem(ingredient.Name, ingredient.Price.ToString()));
            }
        }
        private string gatherInformation()
        {
           string result = "";

            decimal totalPrice = calculatePrice();

           return result = string.Format("Total cost of the pizza is: {0:C}", totalPrice);
        }

        private decimal calculatePrice()
        {
            decimal crustPrice = 0, sizePrice = 0, ingredientPrice = 0, totalPrice = 0;

            crustPrice = Decimal.Parse(crustDropDownList.SelectedValue);
            sizePrice = Decimal.Parse(sizesDropDownList.SelectedValue);

            for (int i = 0; i < ingredientsCheckBox.Items.Count; i++)
            {
                if (ingredientsCheckBox.Items[i].Selected)
                {
                    ingredientPrice += Decimal.Parse(ingredientsCheckBox.Items[i].Value.ToString());
                }
            }

            return totalPrice = crustPrice + sizePrice + ingredientPrice;
        }

        private DTO.Customer createCustomer()
        {
            var customer = new DTO.Customer()
            {
                customerID = Guid.NewGuid(),
                Name = nameTextBox.Text,
                PostalCode = postalTextBox.Text,
                Phone = phoneTextBox.Text,
                Address = addressTextBox.Text,
            };

            return customer;
        }
        private void AddCustomer()
        {
            var customer = createCustomer();
            string errorMessage = "";

            try
            {
                if (customer.Name == "")
                {
                    errorMessage = "Name cannot be empty";
                    throw new CustomerCheckValidationException(errorMessage);
                }
                if (customer.Address == "")
                {
                    errorMessage = "Address cannot be empty";
                    throw new CustomerCheckValidationException(errorMessage);
                }
                if (customer.PostalCode == "")
                {
                    errorMessage = "Postal code cannot be empty";
                    throw new CustomerCheckValidationException(errorMessage);
                }
                if (customer.Phone == "")
                {
                    errorMessage = "Phonenumber cannot be empty";
                    throw new CustomerCheckValidationException(errorMessage);
                }

                pizzaManager.AddCustomer(customer);

                customerLabel.Text = string.Format("{0} have been added!", customer.Name);
            }
            catch (DbEntityValidationException ex)
            {
                foreach (var validations in ex.EntityValidationErrors)
                {
                    foreach (var validation in validations.ValidationErrors)
                    {
                        customerLabel.Text += string.Format("There was an issue with: {0}", validation.PropertyName);
                    }
                }
            }
            catch (CustomerCheckValidationException ex)
            {
                customerLabel.Text = ex.Message;
            }
            catch (Exception ex)
            {
                customerLabel.Text = string.Format("There was an issue with: {0}", ex.Message);
            }
        }

        private DTO.Pizzas createPizza()
        {
            decimal totalCost = calculatePrice();
            var createPizza = new DTO.Pizzas() {
                pizzaID = Guid.NewGuid(),
                customerID = Guid.Parse(customerDropDown.SelectedValue),
                Price = totalCost,
                Completed = 1
            };
            return createPizza;
        }
        private void AddPizza()
        {
            var pizza = createPizza();
            string errorMessage = "";

            try
            {

                if (pizza.Price.ToString() == "")
                {
                    errorMessage = "There must be a price";
                    throw new PizzaCheckValidationException(errorMessage);
                }

                pizzaManager.AddPizza(pizza);

                resultLabel.Text = "Your order have been added";
            }
            catch (DbEntityValidationException ex)
            {
                foreach (var validations in ex.EntityValidationErrors)
                {
                    foreach (var validation in validations.ValidationErrors)
                    {
                        customerLabel.Text += string.Format("There was an issue with: {0}", validation.PropertyName);
                    }
                }
            }
            catch (PizzaCheckValidationException ex)
            {
                customerLabel.Text = ex.Message;
            }
            catch (Exception ex)
            {
                customerLabel.Text = string.Format("There was an issue with: {0}", ex.Message);
            }
        }
        //Event logic
        protected void addCustomer_Click(object sender, EventArgs e)
        {
            AddCustomer();
        }
        protected void orderButton_Click(object sender, EventArgs e)
        {
             AddPizza();
        }
    }
}<file_sep>/Pizza.Presentation/Pizzas.aspx.cs
﻿using System;
using Pizza.Domain;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Pizza.Presentation
{
    public partial class Pizzas : System.Web.UI.Page
    {
        PizzaManager pizzaManager = new PizzaManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                getPizzas();
        }
        private void getPizzas()
        {
            var pizzas = pizzaManager.GetPizzas();
            var uncompletedOrder = new List<DTO.Pizzas>();

            foreach (var pizza in pizzas)
            {
                if(pizza.Completed == 0)
                {
                    uncompletedOrder.Add(pizza);
                }
            }

            pizzaGridView.DataSource = uncompletedOrder;
            
            pizzaGridView.DataBind();
        }
        protected void pizzaGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = pizzaGridView.Rows[index];

            var value = row.Cells[1].Text.ToString();

            Guid pizzaId = Guid.Parse(row.Cells[1].Text.ToString());

            pizzaManager.UpdatePizza(pizzaId);
            getPizzas();
        }
    }
}<file_sep>/Pizza.DTO/Pizzas.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pizza.DTO
{
    public class Pizzas
    {
        public System.Guid pizzaID { get; set; }
        public System.Guid customerID { get; set; }
        public decimal Price { get; set; }
        public Nullable<int> Completed { get; set; }
    }
}
<file_sep>/Pizza.DTO/Customer.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pizza.DTO
{
    public class Customer
    { 
    public System.Guid customerID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string PostalCode { get; set; }
    public string Phone { get; set; }
    }
}
<file_sep>/Pizza.DTO/Ingredients.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pizza.DTO
{
    public class Ingredients
    {
        public System.Guid ingredientID { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }
}
<file_sep>/Pizza.Persistence/PizzaRepository.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pizza.Persistence
{
    public class PizzaRepository
    {
        PizzaEntities db = new PizzaEntities();
        //Get information from the database and sends it to the domain level.
        public List<DTO.Customer> GetCustomers()
        {
            var customers = db.Customer;
            var dtoCustomers = new List<DTO.Customer>();

            foreach (var customer in customers)
            {
                var dtoCustomer = new DTO.Customer() {
                    customerID = customer.customerID,
                    Name = customer.Name,
                    Address = customer.Address,
                    Phone = customer.Phone,
                    PostalCode = customer.PostalCode
                };

                dtoCustomers.Add(dtoCustomer);
            }

            return dtoCustomers;
        }
        public List<DTO.Crusts> GetCrusts()
        {
            var crusts = db.Crusts;
            var dtoCrusts = new List<DTO.Crusts>();

            foreach (var crust in crusts)
            {
                var dtoCrust = new DTO.Crusts() {
                    Name = crust.Name,
                    Price = crust.Price
                };

                dtoCrusts.Add(dtoCrust);
            }

            return dtoCrusts;
        }
        public List<DTO.Ingredients> GetIngredients()
        {
            var ingredients = db.Ingredients;
            var dtoIngredients = new List<DTO.Ingredients>();

            foreach (var ingridient in ingredients)
            {
                var dtoIngridient = new DTO.Ingredients() {
                    Name = ingridient.Name,
                    Price = ingridient.Price
                };

                dtoIngredients.Add(dtoIngridient);
            }

            return dtoIngredients;
        }
        public List<DTO.Size> GetSize()
        {
            var sizes = db.Size;
            var dtoSizes = new List<DTO.Size>();

            foreach (var size in sizes)
            {
                var dtoSize = new DTO.Size() {
                    Name = size.Name,
                    Price = size.Price
                };

                dtoSizes.Add(dtoSize);
            }

            return dtoSizes;
        }
        public List<DTO.Pizzas> GetPizzas()
        {
            var pizzas = db.Pizzas;
            var dtoPizzas = new List<DTO.Pizzas>();

            foreach (var pizza in pizzas)
            {
                var dtoPizza = new DTO.Pizzas()
                {
                    pizzaID = pizza.pizzaID,
                    customerID = pizza.customerID,
                    Price = pizza.Price,
                    Completed = pizza.Completed
                };

                dtoPizzas.Add(dtoPizza);
            }

            return dtoPizzas;
        }
        //Add information to the database
        public void AddCustomer(DTO.Customer newCustomer)
        {
            var customer = new Customer()
            {
                customerID = newCustomer.customerID,
                Name = newCustomer.Name,
                Address = newCustomer.Address,
                PostalCode = newCustomer.PostalCode,
                Phone = newCustomer.Phone
            };

            var dbCustomer = db.Customer;

            dbCustomer.Add(customer);

            db.SaveChanges();
        }
        public void AddPizza(DTO.Pizzas pizza)
        {
            var pizzas = new Pizzas() {
                pizzaID = pizza.pizzaID,
                customerID = pizza.customerID,
                Price = pizza.Price,
                Completed = pizza.Completed
            };

            var dbPizza = db.Pizzas;

            dbPizza.Add(pizzas);

            db.SaveChanges();
        }
        //Update information to the dabase
        public void UpdatePizza(Guid pizza)
        {
            var pizzas = db.Pizzas.Find(pizza);

            pizzas.Completed = 1;

            db.SaveChanges();

        }
    }
}
<file_sep>/Pizza.Domain/PizzaManager.cs
﻿using System;
using Pizza.DTO;
using Pizza.Persistence;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pizza.Domain
{
    public class PizzaManager
    {
        PizzaRepository pizzaRepository = new PizzaRepository();
        //Get information from the persistence layer and sends it to the presentation layer
        public List<DTO.Customer> GetCustomers()
        {
            var customers = pizzaRepository.GetCustomers();

            return customers;
        }
        public List<DTO.Crusts> GetCrusts() {
            List<DTO.Crusts> crusts = pizzaRepository.GetCrusts();
            return crusts;
        }
        public List<DTO.Ingredients> GetIngredients()
        {
            List<DTO.Ingredients> ingredients = pizzaRepository.GetIngredients();
            return ingredients;
        }
        public List<DTO.Size> GetSizes()
        {
            List<DTO.Size> sizes = pizzaRepository.GetSize();
            return sizes;
        }
        public List<DTO.Pizzas> GetPizzas()
        {
            var pizzas = pizzaRepository.GetPizzas();

            return pizzas;
        }
        //Add information to the persistence layer.
        public void AddCustomer(DTO.Customer customer)
        {
            pizzaRepository.AddCustomer(customer);
        }
        public void AddPizza(DTO.Pizzas pizza)
        {
            pizzaRepository.AddPizza(pizza);
        }
        public void UpdatePizza(Guid pizza)
        {
            pizzaRepository.UpdatePizza(pizza);
        }
    }
}
<file_sep>/Pizza.DTO/Size.cs
﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pizza.DTO
{
    public class Size
    {
        public System.Guid sizeID { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }
}
