File tree 3 files changed +71
-2
lines changed
3 files changed +71
-2
lines changed Original file line number Diff line number Diff line change 1
1
package com .example ;
2
2
3
+ import io .micronaut .http .HttpMessage ;
4
+ import io .micronaut .http .HttpResponse ;
5
+ import io .micronaut .http .HttpStatus ;
3
6
import io .micronaut .http .annotation .Controller ;
7
+ import io .micronaut .http .annotation .Delete ;
4
8
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 ;
5
14
import jakarta .inject .Inject ;
15
+ import jdk .jfr .ContentType ;
6
16
7
17
import java .util .List ;
8
18
@@ -17,14 +27,43 @@ public void init() {
17
27
service .initDataBase ();
18
28
}
19
29
20
- @ Get ( "/books" )
30
+ @ Get
21
31
public List <Book > books () {
22
32
return service .findAll ();
23
33
}
24
34
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
+ }
25
58
26
59
@ Get ("/fall" )
27
60
public void fall () {
28
61
service .fall ();
29
62
}
63
+
64
+ @ Get (value = "/test" )
65
+ public String test () {
66
+
67
+ return "HI" ;
68
+ }
30
69
}
Original file line number Diff line number Diff line change 1
1
package com .example ;
2
2
3
3
import io .micronaut .context .annotation .Primary ;
4
+ import io .micronaut .http .HttpMessage ;
4
5
import jakarta .inject .Inject ;
5
6
import jakarta .inject .Singleton ;
6
7
8
+ import javax .transaction .Transactional ;
7
9
import java .util .List ;
8
10
import java .util .NoSuchElementException ;
11
+ import java .util .Optional ;
9
12
10
13
@ Primary
11
14
@ Singleton
@@ -14,6 +17,7 @@ public class BookService {
14
17
@ Inject
15
18
BookRepository repository ;
16
19
20
+ @ Transactional
17
21
public void initDataBase (){
18
22
19
23
Book book = new Book ();
@@ -22,17 +26,41 @@ public void initDataBase(){
22
26
repository .save (book );
23
27
}
24
28
29
+ @ Transactional
25
30
public List <Book > findAll (){
26
31
27
32
return (List <Book >) repository .findAll ();
28
33
}
29
34
30
-
35
+ @ Transactional
31
36
public Book fall (){
32
37
33
38
return repository
34
39
.findById (null )
35
40
.orElseThrow ((NoSuchElementException ::new ));
36
41
37
42
}
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
+ }
38
66
}
Original file line number Diff line number Diff line change 1
1
micronaut :
2
2
application :
3
3
name : demo
4
+ server :
5
+ port : 8081
4
6
netty :
5
7
default :
6
8
allocator :
You can’t perform that action at this time.
0 commit comments