diff --git a/src/main/java/com/example/demo/Tag.java b/src/main/java/com/example/demo/Tag.java new file mode 100644 index 000000000..644408ffa --- /dev/null +++ b/src/main/java/com/example/demo/Tag.java @@ -0,0 +1,40 @@ +package com.example.demo; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Tag { + @Id + @GeneratedValue + Long id; + String name; + + public Tag() { + } + + public Tag(Long id, String name) { + this.id = id; + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} + + + diff --git a/src/main/java/com/example/demo/TagController.java b/src/main/java/com/example/demo/TagController.java new file mode 100644 index 000000000..bcafe7c19 --- /dev/null +++ b/src/main/java/com/example/demo/TagController.java @@ -0,0 +1,41 @@ +package com.example.demo; + +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.List; + +@RestController +@RequestMapping("/tagController") +public class TagController { + + @Autowired + private TagService service; + + @GetMapping(value = "/read/{id}") + public ResponseEntity read(@PathVariable Long id){ + return new ResponseEntity<>(service.read(id), HttpStatus.OK); + } + + @GetMapping(value = "/read") + public ResponseEntity> readAll(){ + return new ResponseEntity<>(service.readAll(), HttpStatus.OK); + } + + @PostMapping(value = "/create") + public ResponseEntity create(@RequestBody Tag tag){ + return new ResponseEntity<>(service.create(tag), HttpStatus.CREATED); + } + + @PutMapping(value = "/update/{id}") + public ResponseEntity update(@PathVariable Long id, @RequestBody Tag tag){ + return new ResponseEntity<>(service.update(id, tag), HttpStatus.OK); + } + + @DeleteMapping(value = "/delete/{id}") + public ResponseEntity delete(@PathVariable Long id){ + return new ResponseEntity<>(service.delete(id), HttpStatus.OK); + } +} diff --git a/src/main/java/com/example/demo/TagRepository.java b/src/main/java/com/example/demo/TagRepository.java new file mode 100644 index 000000000..dd217c9f4 --- /dev/null +++ b/src/main/java/com/example/demo/TagRepository.java @@ -0,0 +1,6 @@ +package com.example.demo; + +import org.springframework.data.repository.CrudRepository; + +public interface TagRepository extends CrudRepository { +} diff --git a/src/main/java/com/example/demo/TagService.java b/src/main/java/com/example/demo/TagService.java new file mode 100644 index 000000000..5e8f9c8d4 --- /dev/null +++ b/src/main/java/com/example/demo/TagService.java @@ -0,0 +1,39 @@ +package com.example.demo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class TagService { + @Autowired + private TagRepository repository; + + public Tag create(Tag tag){ return repository.save(tag); } + + public Tag read(Long id){ return repository.findById(id).get(); } + + public List readAll(){ + Iterable tagIterable = repository.findAll(); + List result = new ArrayList<>(); + tagIterable.forEach(result::add); + return result; + } + + public Tag update(Long id, Tag newTag){ + Tag tagInDatabase = read(id); + tagInDatabase.setName(newTag.getName()); + return repository.save(tagInDatabase); + } + + public Tag delete(Tag tag){ + repository.delete(tag); + return tag; + } + + public Tag delete(Long id){ + return delete(read(id)); + } +} diff --git a/target/classes/com/example/demo/Tag.class b/target/classes/com/example/demo/Tag.class new file mode 100644 index 000000000..37a9206d3 Binary files /dev/null and b/target/classes/com/example/demo/Tag.class differ diff --git a/target/classes/com/example/demo/TagController.class b/target/classes/com/example/demo/TagController.class new file mode 100644 index 000000000..acd0545ca Binary files /dev/null and b/target/classes/com/example/demo/TagController.class differ diff --git a/target/classes/com/example/demo/TagRepository.class b/target/classes/com/example/demo/TagRepository.class new file mode 100644 index 000000000..a45b7d0ba Binary files /dev/null and b/target/classes/com/example/demo/TagRepository.class differ diff --git a/target/classes/com/example/demo/TagService.class b/target/classes/com/example/demo/TagService.class new file mode 100644 index 000000000..8f60f04ab Binary files /dev/null and b/target/classes/com/example/demo/TagService.class differ