<repo_name>kadireker/customerList<file_sep>/src/main/webapp/resources/js/scripts.js
$(document)
		.ready(
				function() {
					$("#btnAddCustomer").click(function() {
						var Person = {
							name : $('#first_name').val(),
							surname : $('#last_name').val(),
							phone : $('#phone').val(),
							captcha : $('#captcha').val()
						};

						$.ajax({
							url : "/myapp/customer/save",
							context : document.body,
							type : 'POST',
							data : Person,
							success : function() {
								window.location = "?state=1";
							},
							error : function() {
								alert("Captcha does not match!!!");
							}

						});
					});

					$('#customerDelete').on('show.bs.modal', function(e) {
						$id = $(e.relatedTarget).attr('data-refid');
						$("#btnDeleteCustomer").click(function() {
							$.ajax({
								url : '/myapp/customer/delete?id=' + $id,
								type : 'POST',
								success : function(result) {
									window.location = "?state=2";
								},
								error : function() {
									alert("failure");
								}
							});
						});
					});

					$('#customerEdit').on(
							'show.bs.modal',
							function(e) {
								$id = $(e.relatedTarget).attr('data-refid');

								$.get('/myapp/customer/check/' + $id, function(
										customer) {
									$('#first_name_edit').val(customer.name);
									$('#last_name_edit').val(customer.surname);
									$('#phone_edit').val(customer.phone);
								});

								$("#btnEditCustomer").click(function() {
									var Person = {
										name : $('#first_name_edit').val(),
										surname : $('#last_name_edit').val(),
										phone : $('#phone_edit').val()
									};

									$.ajax({
										url : "/myapp/customer/update/" + $id,
										context : document.body,
										type : 'POST',
										data : Person,
										success : function(result) {
											window.location = "?state=3";
										},
										error : function() {
											alert("failure");
										}
									});
								});
							});

					$("#btnRefresh").click(function() {
						window.location = "/myapp/customer";
					});

					$.validate();
					$("#phone, #phone_edit")
							.keyup(
									function() {
										var val = $(this).val();
										var bas = val.length > 15 ? val.substr(
												0, 15) : val;
										var son = val.length > 15 ? val.substr(
												15, val.length) : "";
										val = val.length > 15 ? String(bas
												.replace(/[\D]/g, ''))
												+ String(son.replace(
														/[^0-9\s\-]/g, ''))
												: String(val.replace(/[\D]/g,
														''));
										var str = "";
										if (val.length == 11) {
											str = "("
													+ val.substr(0, 3)
													+ ") "
													+ val.substr(3, 3)
													+ " "
													+ val.substr(6, 2)
													+ " "
													+ val.substr(8, 2)
													+ " - "
													+ val
															.substr(10,
																	val.length);
										} else if (val.length >= 8) {
											str = "(" + val.substr(0, 3) + ") "
													+ val.substr(3, 3) + " "
													+ val.substr(6, 2) + " "
													+ val.substr(8, val.length);
										} else if (val.length >= 6) {
											str = "(" + val.substr(0, 3) + ") "
													+ val.substr(3, 3) + " "
													+ val.substr(6, val.length);
										} else if (val.length >= 3) {
											str = "(" + val.substr(0, 3) + ") "
													+ val.substr(3, val.length);
										} else {
											str = val;
										}
										$(this).val(str);
									});
				});
<file_sep>/src/main/java/com/enerjiyazilim/myapp/HomeController.java
package com.enerjiyazilim.myapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpSession;

import com.enerjiyazilim.model.Customer;
import com.enerjiyazilim.service.CustomerDAO;

/**
 * 
 * Handles requests for the application home page. 
 * Spring Mvc Controller Class
 * created by <NAME> 23/08/2015
 * 
 */
@Controller
public class HomeController {

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home() {
		return "home";
	}

	@RequestMapping(value = "/about", method = RequestMethod.GET)
	public String about() {
		return "about";
	}

	/**
	 * List of Customer with GET method
	 * 
	 * @return list of customers
	 */
	@Autowired
	private CustomerDAO customerDao;
	@Autowired
	private MongoTemplate mongoTemplate;
	public static final String COLLECTION = "customer";

	@RequestMapping(value = "/customer", method = RequestMethod.GET)
	public String getCustomerList(ModelMap model) {
		model.addAttribute("customerList", customerDao.listCustomer());
		return "person";
	}

	/**
	 * Creating the customer in the DB through the service layer which is 'POST'
	 * method
	 * 
	 * @model customer
	 * @return
	 */
	@RequestMapping(value = "/customer/save", method = RequestMethod.POST)
	public View createPerson(@ModelAttribute Customer customer, ModelMap model,
			HttpSession session) {
		String captcha = (String) session.getAttribute("CAPTCHA");

		if (captcha == null
				|| (captcha != null && !captcha.equals(customer.getCaptcha()))) {
			customer.setCaptcha("");
			model.addAttribute("state", "Captcha does not match");
			return new RedirectView("redirect:customer");
		}

		if (StringUtils.hasText(customer.getId())) {
			customerDao.updateCustomer(customer);
		} else {
			customerDao.addCustomer(customer);
		}
		return new RedirectView("/myapp/customer");
	}

	/**
	 * Delete customer from the DB through a service layer which is 'POST'
	 * method
	 *
	 * @model customer
	 */
	@RequestMapping(value = "/customer/delete", method = RequestMethod.POST)
	public View deleteCustomer(@ModelAttribute Customer customer, ModelMap model) {
		customerDao.deleteCustomer(customer);
		return new RedirectView("/myapp/customer");
	}

	/**
	 * Uptading the customer in the DB through the service layer which is send
	 * by user 'id' 'GET' method
	 *
	 * @param id
	 * @return customer
	 */
	@RequestMapping(value = "/customer/check/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Customer getById(@PathVariable String id) {
		return customerDao.getCustomer(id);
	}

	@RequestMapping(value = "/customer/update/{id}", method = RequestMethod.POST)
	public View updateById(@ModelAttribute Customer customer,
			@PathVariable String id) {
		Customer existingUser = customerDao.getCustomer(id);

		if (existingUser != null) {
			existingUser.setName(customer.getName());
			existingUser.setSurname(customer.getSurname());
			existingUser.setPhone(customer.getPhone());
			mongoTemplate.save(existingUser, COLLECTION);
		}

		return new RedirectView("/myapp/customer");
	}

}
<file_sep>/README.md
# This project about managed client list. You can add new customer, delete customer or edit customer. I am using Spring MVC and MongoDB. In addition i benefit AJAX and Bootstrap techonology.
<file_sep>/src/main/java/com/enerjiyazilim/service/CustomerDAOImpl.java
package com.enerjiyazilim.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

import com.enerjiyazilim.model.Customer;

/**
 * Service Layer implemantion
 *
 * Created by kadireker on 24/08/2015.
 */
@Repository
public class CustomerDAOImpl implements CustomerDAO {
	@Autowired
	private MongoTemplate mongoTemplate;
	public static final String COLLECTION = "customer";

	public void addCustomer(Customer customer) {
		if (!mongoTemplate.collectionExists(Customer.class)) {
			mongoTemplate.createCollection(Customer.class);
		}
		customer.setId(UUID.randomUUID().toString());
		mongoTemplate.insert(customer, COLLECTION);
	}

	public List<Customer> listCustomer() {
		return mongoTemplate.findAll(Customer.class, COLLECTION);
	}

	public void deleteCustomer(Customer customer) {
		mongoTemplate.remove(customer, COLLECTION);
	}

	public void updateCustomer(Customer customer) {
		mongoTemplate.insert(customer, COLLECTION);
	}

	public Customer getCustomer(String id) {
		return mongoTemplate.findById(id, Customer.class);
	}

}