Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding customer #8

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Thumbs.db
.project
.classpath
.idea
.mvn
*.iml
atlassian-ide-plugin.xml
target
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--WebJars-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package guru.springframework.controllers;

import guru.springframework.domain.Customer;
import guru.springframework.services.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* Created by jt on 11/15/15.
*/
@RequestMapping("/customer")
@Controller
public class CustomerController {

private CustomerService customerService;

@Autowired
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}

@RequestMapping({"/list", "/"})
public String listCustomers(Model model){
model.addAttribute("customers", customerService.listAll());
return "customer/list";
}

@RequestMapping("/show/{id}")
public String showCustomer(@PathVariable Integer id, Model model){
model.addAttribute("customer", customerService.getById(id));
return "customer/show";
}

@RequestMapping("/edit/{id}")
public String edit(@PathVariable Integer id, Model model){
model.addAttribute("customer", customerService.getById(id));
return "customer/customerform";
}

@RequestMapping("/new")
public String newCustomer(Model model){
model.addAttribute("customer", new Customer());
return "customer/customerform";
}

@RequestMapping(method = RequestMethod.POST)
public String saveOrUpdate(Customer customer){
Customer newCustomer = customerService.saveOrUpdate(customer);
return "redirect:customer/show/" + newCustomer.getId();
}

@RequestMapping("/delete/{id}")
public String delete(@PathVariable Integer id){
customerService.delete(id);
return "redirect:/customer/list";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package guru.springframework.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* Created by jt on 11/6/15.
*/
@Controller
public class IndexController {

@RequestMapping("/")
public String index(){
return "index";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package guru.springframework.controllers;

import guru.springframework.domain.Product;
import guru.springframework.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* Created by jt on 11/6/15.
*/
@Controller
public class ProductController {

private ProductService productService;

@Autowired
public void setProductService(ProductService productService) {
this.productService = productService;
}

@RequestMapping("/product/list")
public String listProducts(Model model){
model.addAttribute("products", productService.listAll());
return "product/list";
}

@RequestMapping("/product/show/{id}")
public String getProduct(@PathVariable Integer id, Model model){
model.addAttribute("product", productService.getById(id));
return "product/show";
}

@RequestMapping("product/edit/{id}")
public String edit(@PathVariable Integer id, Model model){
model.addAttribute("product", productService.getById(id));
return "product/productform";
}

@RequestMapping("/product/new")
public String newProduct(Model model){
model.addAttribute("product", new Product());
return "product/productform";
}

@RequestMapping(value = "/product", method = RequestMethod.POST)
public String saveOrUpdateProduct(Product product){
Product savedProduct = productService.saveOrUpdate(product);
return "redirect:/product/show/" + savedProduct.getId();
}

@RequestMapping("/product/delete/{id}")
public String delete(@PathVariable Integer id){
productService.delete(id);
return "redirect:/product/list";
}
}
100 changes: 100 additions & 0 deletions src/main/java/guru/springframework/domain/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package guru.springframework.domain;

/**
* Created by jt on 11/14/15.
*/
public class Customer implements DomainObject {

private Integer id;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String zipCode;

@Override
public Integer getId() {
return id;
}

@Override
public void setId(Integer id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getAddressLine1() {
return addressLine1;
}

public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}

public String getAddressLine2() {
return addressLine2;
}

public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getZipCode() {
return zipCode;
}

public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
}
11 changes: 11 additions & 0 deletions src/main/java/guru/springframework/domain/DomainObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package guru.springframework.domain;

/**
* Created by jt on 11/14/15.
*/
public interface DomainObject {

Integer getId();

void setId(Integer id);
}
45 changes: 45 additions & 0 deletions src/main/java/guru/springframework/domain/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package guru.springframework.domain;

import java.math.BigDecimal;

/**
* Created by jt on 11/6/15.
*/
public class Product implements DomainObject{
private Integer id;
private String description;
private BigDecimal price;
private String imageUrl;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public BigDecimal getPrice() {
return price;
}

public void setPrice(BigDecimal price) {
this.price = price;
}

public String getImageUrl() {
return imageUrl;
}

public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package guru.springframework.services;

import guru.springframework.domain.DomainObject;

import java.util.*;

/**
* Created by jt on 11/14/15.
*/
public abstract class AbstractMapService {
protected Map<Integer, DomainObject> domainMap;

public AbstractMapService() {
domainMap = new HashMap<>();
loadDomainObjects();
}

public List<DomainObject> listAll() {
return new ArrayList<>(domainMap.values());
}

public DomainObject getById(Integer id) {
return domainMap.get(id);
}

public DomainObject saveOrUpdate(DomainObject domainObject) {
if (domainObject != null){

if (domainObject.getId() == null){
domainObject.setId(getNextKey());
}
domainMap.put(domainObject.getId(), domainObject);

return domainObject;
} else {
throw new RuntimeException("Object Can't be null");
}
}

public void delete(Integer id) {
domainMap.remove(id);
}

private Integer getNextKey(){
return Collections.max(domainMap.keySet()) + 1;
}

protected abstract void loadDomainObjects();

}
16 changes: 16 additions & 0 deletions src/main/java/guru/springframework/services/CRUDService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package guru.springframework.services;

import java.util.List;

/**
* Created by jt on 11/14/15.
*/
public interface CRUDService<T> {
List<?> listAll();

T getById(Integer id);

T saveOrUpdate(T domainObject);

void delete(Integer id);
}
Loading