Skip to content
Merged
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
40 changes: 40 additions & 0 deletions src/main/java/com/example/demo/Tag.java
Original file line number Diff line number Diff line change
@@ -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;
}
}



41 changes: 41 additions & 0 deletions src/main/java/com/example/demo/TagController.java
Original file line number Diff line number Diff line change
@@ -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<Tag> read(@PathVariable Long id){
return new ResponseEntity<>(service.read(id), HttpStatus.OK);
}

@GetMapping(value = "/read")
public ResponseEntity<List<Tag>> readAll(){
return new ResponseEntity<>(service.readAll(), HttpStatus.OK);
}

@PostMapping(value = "/create")
public ResponseEntity<Tag> create(@RequestBody Tag tag){
return new ResponseEntity<>(service.create(tag), HttpStatus.CREATED);
}

@PutMapping(value = "/update/{id}")
public ResponseEntity<Tag> update(@PathVariable Long id, @RequestBody Tag tag){
return new ResponseEntity<>(service.update(id, tag), HttpStatus.OK);
}

@DeleteMapping(value = "/delete/{id}")
public ResponseEntity<Tag> delete(@PathVariable Long id){
return new ResponseEntity<>(service.delete(id), HttpStatus.OK);
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/example/demo/TagRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.demo;

import org.springframework.data.repository.CrudRepository;

public interface TagRepository extends CrudRepository<Tag, Long> {
}
39 changes: 39 additions & 0 deletions src/main/java/com/example/demo/TagService.java
Original file line number Diff line number Diff line change
@@ -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<Tag> readAll(){
Iterable<Tag> tagIterable = repository.findAll();
List<Tag> 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));
}
}
Binary file added target/classes/com/example/demo/Tag.class
Binary file not shown.
Binary file not shown.
Binary file added target/classes/com/example/demo/TagRepository.class
Binary file not shown.
Binary file added target/classes/com/example/demo/TagService.class
Binary file not shown.