Skip to content

add Customer: #17

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.javastudy.deliveryservice.mvc.dao;

import com.javastudy.deliveryservice.mvc.entity.Customer;
import java.util.List;

/**
*
* @author Tanya
*/
public interface CustomerDAO {
public void persist(Customer customer);

public List<Customer> findAll();

public void remove(Customer customer);

public Customer findByID(Long id);

public Customer merge(Customer customer);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.javastudy.deliveryservice.mvc.dao;

import com.javastudy.deliveryservice.mvc.entity.Customer;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
*
* @author Tanya
*/
@Repository
public class CustomerDAOImpl implements CustomerDAO {

@Autowired
EntityManager manager;

public void persist(Customer customer) {
manager.getTransaction().begin();
manager.persist(customer);
manager.getTransaction().commit();
}

public List<Customer> findAll() {
Query query = manager.createQuery("SELECT c FROM Customer c");
List<Customer> list = query.getResultList();
return list;
}

public void remove(Customer customer) {
manager.getTransaction().begin();
manager.remove(manager.merge(customer));
manager.getTransaction().commit();
}

public Customer findByID(Long id) {
Customer customer = manager.find(Customer.class, id);
return customer;
}

public Customer merge(Customer customer) {
manager.getTransaction().begin();
customer = manager.merge(customer);
manager.getTransaction().commit();
return customer;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.javastudy.deliveryservice.mvc.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;

/**
*
* @author Tanya
*/
@Entity
@Table(name = "customers",
uniqueConstraints = @UniqueConstraint(columnNames = {"login", "phone", "email"})
)
public class Customer implements Serializable {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "login")
@Size(min = 6, max = 16)
@NotNull
private String login;

@Column(name = "password")
@Size(min = 6, max = 16)
@NotNull
private String password;

@Column(name = "name")
@Size(min = 3, max = 16)
@NotNull
private String name;

@Column(name = "phone")
@Size(min = 4, max = 20)
@NotNull
private String phone;

@Column(name = "email")
@Email
@Size(min = 5, max = 64)
@NotNull
private String email;

/*
@OneToOne
@JoinColumn(name = "id_adress", referencedColumnName = "id")
private Adress adress;

*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.javastudy.deliveryservice.mvc.service;

import com.javastudy.deliveryservice.mvc.entity.Customer;
import java.util.List;

/**
*
* @author Tanya
*/
public interface CustomerService {

public void persist(Customer customer);

public List<Customer> findAll();

public void remove(Customer customer);

public Customer findByID(Long id);

public Customer merge(Customer customer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.javastudy.deliveryservice.mvc.service;

import com.javastudy.deliveryservice.mvc.dao.CustomerDAO;
import com.javastudy.deliveryservice.mvc.entity.Customer;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
*
* @author Tanya
*/
@Service
public class CustomerServiceImpl implements CustomerService {

@Autowired
CustomerDAO customerDAO;

public void persist(Customer customer) {
customerDAO.persist(customer);
}

public List<Customer> findAll() {
return customerDAO.findAll();
}

public void remove(Customer customer) {
customerDAO.remove(customer);
}

public Customer findByID(Long id) {
return customerDAO.findByID(id);
}

public Customer merge(Customer customer) {
return customerDAO.merge(customer);
}

}