Skip to content

Commit 3a04043

Browse files
committed
another test endpoints
1 parent 30450f0 commit 3a04043

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

src/main/java/com/example/BookController.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package com.example;
22

3+
import io.micronaut.http.HttpMessage;
4+
import io.micronaut.http.HttpResponse;
5+
import io.micronaut.http.HttpStatus;
36
import io.micronaut.http.annotation.Controller;
7+
import io.micronaut.http.annotation.Delete;
48
import io.micronaut.http.annotation.Get;
9+
import io.micronaut.http.annotation.PathVariable;
10+
import io.micronaut.http.annotation.Post;
11+
import io.micronaut.http.annotation.Put;
12+
import io.micronaut.http.annotation.QueryValue;
13+
import io.micronaut.http.annotation.RequestBean;
514
import jakarta.inject.Inject;
15+
import jdk.jfr.ContentType;
616

717
import java.util.List;
818

@@ -17,14 +27,43 @@ public void init() {
1727
service.initDataBase();
1828
}
1929

20-
@Get("/books")
30+
@Get
2131
public List<Book> books() {
2232
return service.findAll();
2333
}
2434

35+
@Post
36+
public HttpMessage create(Book book) {
37+
service.create(book);
38+
39+
return HttpResponse.status(HttpStatus.CREATED);
40+
}
41+
42+
@Delete("{id}")
43+
public HttpMessage delete(@PathVariable Long id){
44+
45+
service.delete(id);
46+
47+
return HttpResponse.status(HttpStatus.NO_CONTENT);
48+
}
49+
50+
51+
@Put("{id}")
52+
public HttpMessage update(@PathVariable Long id, @QueryValue String name) {
53+
54+
service.updateName(id, name);
55+
56+
return HttpResponse.status(HttpStatus.ACCEPTED);
57+
}
2558

2659
@Get("/fall")
2760
public void fall() {
2861
service.fall();
2962
}
63+
64+
@Get(value = "/test")
65+
public String test() {
66+
67+
return "HI";
68+
}
3069
}

src/main/java/com/example/BookService.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.example;
22

33
import io.micronaut.context.annotation.Primary;
4+
import io.micronaut.http.HttpMessage;
45
import jakarta.inject.Inject;
56
import jakarta.inject.Singleton;
67

8+
import javax.transaction.Transactional;
79
import java.util.List;
810
import java.util.NoSuchElementException;
11+
import java.util.Optional;
912

1013
@Primary
1114
@Singleton
@@ -14,6 +17,7 @@ public class BookService {
1417
@Inject
1518
BookRepository repository;
1619

20+
@Transactional
1721
public void initDataBase(){
1822

1923
Book book = new Book();
@@ -22,17 +26,41 @@ public void initDataBase(){
2226
repository.save(book);
2327
}
2428

29+
@Transactional
2530
public List<Book> findAll(){
2631

2732
return (List<Book>) repository.findAll();
2833
}
2934

30-
35+
@Transactional
3136
public Book fall(){
3237

3338
return repository
3439
.findById(null)
3540
.orElseThrow((NoSuchElementException::new));
3641

3742
}
43+
44+
@Transactional
45+
public void create(Book book) {
46+
repository.save(book);
47+
}
48+
49+
@Transactional
50+
public void delete(Long id) {
51+
52+
repository.deleteById(id);
53+
}
54+
55+
@Transactional
56+
public void updateName(Long id, String title) {
57+
58+
Optional<Book> book = repository.findById(id);
59+
60+
if(book.isPresent()) {
61+
Book bookUpdated = book.get();
62+
bookUpdated.setTitle(title);
63+
repository.update(bookUpdated);
64+
}
65+
}
3866
}

src/main/resources/application.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
micronaut:
22
application:
33
name: demo
4+
server:
5+
port: 8081
46
netty:
57
default:
68
allocator:

0 commit comments

Comments
 (0)