소스 검색

done all except tests

Jonathan Hinds 6 년 전
부모
커밋
173e000b6d

+ 48
- 0
src/main/java/io/zipcoder/crudapp/Person.java 파일 보기

@@ -0,0 +1,48 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import javax.persistence.Entity;
4
+import javax.persistence.GeneratedValue;
5
+import javax.persistence.GenerationType;
6
+import javax.persistence.Id;
7
+
8
+@Entity
9
+public class Person {
10
+
11
+    @Id
12
+    @GeneratedValue(strategy = GenerationType.AUTO)
13
+    private int id;
14
+    private String firstname;
15
+    private String lastname;
16
+
17
+    public Person() {
18
+    }
19
+
20
+    public Person(String firstname, String lastname) {
21
+        this.firstname = firstname;
22
+        this.lastname = lastname;
23
+    }
24
+
25
+    public int getId() {
26
+        return id;
27
+    }
28
+
29
+    public void setId(int id) {
30
+        this.id = id;
31
+    }
32
+
33
+    public String getFirstname() {
34
+        return firstname;
35
+    }
36
+
37
+    public void setFirstname(String firstname) {
38
+        this.firstname = firstname;
39
+    }
40
+
41
+    public String getLastname() {
42
+        return lastname;
43
+    }
44
+
45
+    public void setLastname(String lastname) {
46
+        this.lastname = lastname;
47
+    }
48
+}

+ 44
- 0
src/main/java/io/zipcoder/crudapp/PersonController.java 파일 보기

@@ -0,0 +1,44 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.web.bind.annotation.*;
5
+
6
+import java.util.List;
7
+
8
+@RestController
9
+public class PersonController {
10
+
11
+    @Autowired
12
+    PersonService serv;
13
+
14
+    public PersonController() {
15
+    }
16
+
17
+    @PostMapping("/people/{firstName}/{lastName}")
18
+    public Person createPerson(@PathVariable String firstName, @PathVariable String lastName){
19
+        Person person = new Person(firstName, lastName);
20
+        serv.save(person);
21
+        return serv.getOne(person.getId());
22
+    }
23
+
24
+    @GetMapping("/people/{id}")
25
+    public Person getPerson(@PathVariable int id){
26
+        return serv.getOne(id);
27
+    }
28
+
29
+    @GetMapping("/people")
30
+    public List<Person> getPersonList(){
31
+        return serv.findAll();
32
+    }
33
+
34
+    @PutMapping("/people/{id}/{firstName}/{lastName}")
35
+    public Person updatePerson(@RequestBody Person p){
36
+        return serv.save(p);
37
+    }
38
+
39
+    @DeleteMapping("/people/{id}")
40
+    public void DeletePerson(@PathVariable int id){
41
+        serv.delete(id);
42
+    }
43
+
44
+}

+ 8
- 0
src/main/java/io/zipcoder/crudapp/PersonRepo.java 파일 보기

@@ -0,0 +1,8 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.data.repository.CrudRepository;
4
+import org.springframework.stereotype.Repository;
5
+
6
+@Repository
7
+public interface PersonRepo extends CrudRepository<Person, Integer> {
8
+}

+ 33
- 0
src/main/java/io/zipcoder/crudapp/PersonService.java 파일 보기

@@ -0,0 +1,33 @@
1
+package io.zipcoder.crudapp;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.stereotype.Service;
5
+
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+
9
+@Service
10
+public class PersonService {
11
+
12
+    @Autowired
13
+    PersonRepo repo;
14
+
15
+    public List<Person> findAll(){
16
+        List<Person> people = new ArrayList<>();
17
+        repo.findAll().forEach(people::add);
18
+        return people;
19
+    }
20
+
21
+    public Person getOne(int id) {
22
+        return repo.findOne(id);
23
+    }
24
+
25
+    public Person save(Person p) {
26
+        repo.save(p);
27
+        return getOne(p.getId());
28
+    }
29
+
30
+    public void delete(int id){
31
+        repo.delete(id);
32
+    }
33
+}