package com.sebworks.addressbook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by seb on 12.08.2015.
 */
@RestController
@RequestMapping("/api")
public class Controller {

	@Autowired
	ContactRepository repository;

	@RequestMapping("/all")
	public List<Contact> findAll(){
		Iterable<Contact> all = repository.findAll();
		ArrayList list = new ArrayList();
		for (Contact contact : all) {
			list.add(contact);
		}
		return list;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ResponseEntity save(@RequestBody Contact contact){
		try {
			if(contact.getName().isEmpty() || contact.getSurname().isEmpty() || contact.getCity().isEmpty() || contact.getPhone().isEmpty()){
				return ResponseEntity.badRequest().build(); //400
			}
			if(contact.getId()!=null && contact.getId().trim().isEmpty())
				contact.setId(null);
			repository.save(contact);
			System.out.println("saved. current db:");
			repository.findAll().forEach(c -> System.out.println(c.toString()));
			return ResponseEntity.noContent().build(); //204
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
		}
	}

	@RequestMapping("/delete/{id}")
	public ResponseEntity delete(@PathVariable("id") String id){
		System.out.println("delete: "+id);
		repository.delete(id);
		return ResponseEntity.noContent().build();
	}

}

@@ -290,16 +290,254 @@
 t;%0A%0A/**%0A
+ * Can be tested by%0A * curl -X POST -d '%7B%22name%22: %22rocky%22, %22surname%22: %22balboa%22, %22city%22: %22philadelphia%22, %22phone%22: %2218001234567%22%7D' -v -H %22Content-Type:application/json%22 http://localhost:8080/api/save%0A * curl http://localhost:8080/api/all%0A *%0A
  * Creat

Add example curl commands for test