26
26
import com .iluwatar .cqrs .domain .model .Author ;
27
27
import com .iluwatar .cqrs .domain .model .Book ;
28
28
import com .iluwatar .cqrs .util .HibernateUtil ;
29
- import org .hibernate .Query ;
30
- import org .hibernate .Session ;
31
29
import org .hibernate .SessionFactory ;
32
30
33
31
/**
@@ -39,9 +37,9 @@ public class CommandServiceImpl implements ICommandService {
39
37
private SessionFactory sessionFactory = HibernateUtil .getSessionFactory ();
40
38
41
39
private Author getAuthorByUsername (String username ) {
42
- Author author = null ;
43
- try (Session session = sessionFactory .openSession ()) {
44
- Query query = session .createQuery ("from Author where username=:username" );
40
+ Author author ;
41
+ try (var session = sessionFactory .openSession ()) {
42
+ var query = session .createQuery ("from Author where username=:username" );
45
43
query .setParameter ("username" , username );
46
44
author = (Author ) query .uniqueResult ();
47
45
}
@@ -53,9 +51,9 @@ private Author getAuthorByUsername(String username) {
53
51
}
54
52
55
53
private Book getBookByTitle (String title ) {
56
- Book book = null ;
57
- try (Session session = sessionFactory .openSession ()) {
58
- Query query = session .createQuery ("from Book where title=:title" );
54
+ Book book ;
55
+ try (var session = sessionFactory .openSession ()) {
56
+ var query = session .createQuery ("from Book where title=:title" );
59
57
query .setParameter ("title" , title );
60
58
book = (Book ) query .uniqueResult ();
61
59
}
@@ -68,8 +66,8 @@ private Book getBookByTitle(String title) {
68
66
69
67
@ Override
70
68
public void authorCreated (String username , String name , String email ) {
71
- Author author = new Author (username , name , email );
72
- try (Session session = sessionFactory .openSession ()) {
69
+ var author = new Author (username , name , email );
70
+ try (var session = sessionFactory .openSession ()) {
73
71
session .beginTransaction ();
74
72
session .save (author );
75
73
session .getTransaction ().commit ();
@@ -78,9 +76,9 @@ public void authorCreated(String username, String name, String email) {
78
76
79
77
@ Override
80
78
public void bookAddedToAuthor (String title , double price , String username ) {
81
- Author author = getAuthorByUsername (username );
82
- Book book = new Book (title , price , author );
83
- try (Session session = sessionFactory .openSession ()) {
79
+ var author = getAuthorByUsername (username );
80
+ var book = new Book (title , price , author );
81
+ try (var session = sessionFactory .openSession ()) {
84
82
session .beginTransaction ();
85
83
session .save (book );
86
84
session .getTransaction ().commit ();
@@ -89,9 +87,9 @@ public void bookAddedToAuthor(String title, double price, String username) {
89
87
90
88
@ Override
91
89
public void authorNameUpdated (String username , String name ) {
92
- Author author = getAuthorByUsername (username );
90
+ var author = getAuthorByUsername (username );
93
91
author .setName (name );
94
- try (Session session = sessionFactory .openSession ()) {
92
+ try (var session = sessionFactory .openSession ()) {
95
93
session .beginTransaction ();
96
94
session .update (author );
97
95
session .getTransaction ().commit ();
@@ -100,9 +98,9 @@ public void authorNameUpdated(String username, String name) {
100
98
101
99
@ Override
102
100
public void authorUsernameUpdated (String oldUsername , String newUsername ) {
103
- Author author = getAuthorByUsername (oldUsername );
101
+ var author = getAuthorByUsername (oldUsername );
104
102
author .setUsername (newUsername );
105
- try (Session session = sessionFactory .openSession ()) {
103
+ try (var session = sessionFactory .openSession ()) {
106
104
session .beginTransaction ();
107
105
session .update (author );
108
106
session .getTransaction ().commit ();
@@ -111,9 +109,9 @@ public void authorUsernameUpdated(String oldUsername, String newUsername) {
111
109
112
110
@ Override
113
111
public void authorEmailUpdated (String username , String email ) {
114
- Author author = getAuthorByUsername (username );
112
+ var author = getAuthorByUsername (username );
115
113
author .setEmail (email );
116
- try (Session session = sessionFactory .openSession ()) {
114
+ try (var session = sessionFactory .openSession ()) {
117
115
session .beginTransaction ();
118
116
session .update (author );
119
117
session .getTransaction ().commit ();
@@ -122,9 +120,9 @@ public void authorEmailUpdated(String username, String email) {
122
120
123
121
@ Override
124
122
public void bookTitleUpdated (String oldTitle , String newTitle ) {
125
- Book book = getBookByTitle (oldTitle );
123
+ var book = getBookByTitle (oldTitle );
126
124
book .setTitle (newTitle );
127
- try (Session session = sessionFactory .openSession ()) {
125
+ try (var session = sessionFactory .openSession ()) {
128
126
session .beginTransaction ();
129
127
session .update (book );
130
128
session .getTransaction ().commit ();
@@ -133,9 +131,9 @@ public void bookTitleUpdated(String oldTitle, String newTitle) {
133
131
134
132
@ Override
135
133
public void bookPriceUpdated (String title , double price ) {
136
- Book book = getBookByTitle (title );
134
+ var book = getBookByTitle (title );
137
135
book .setPrice (price );
138
- try (Session session = sessionFactory .openSession ()) {
136
+ try (var session = sessionFactory .openSession ()) {
139
137
session .beginTransaction ();
140
138
session .update (book );
141
139
session .getTransaction ().commit ();
0 commit comments