entire services in transactions

This commit is contained in:
Stepan Usatiuk
2024-01-04 17:41:11 +01:00
parent 72161902f9
commit 50cc02351d
9 changed files with 14 additions and 6 deletions

View File

@@ -14,7 +14,7 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@ApiResponse( @ApiResponse(
responseCode = "409", responseCode = "409",
description = "Conflict - creating/updating what is requested would conflict with existing data", description = "Conflict - creating/updating what is requested would conflict with existing data (e.g. usernames must be unique)",
content = @Content( content = @Content(
mediaType = "application/json", mediaType = "application/json",
schema = @Schema(implementation = ErrorTo.class) schema = @Schema(implementation = ErrorTo.class)

View File

@@ -29,7 +29,7 @@ public class Message {
private Person author; private Person author;
@CreationTimestamp @CreationTimestamp
private Instant createdAt; private Instant createdAt = Instant.now();
@Lob @Lob
@NotBlank(message = "Message can't be empty") @NotBlank(message = "Message can't be empty")

View File

@@ -30,5 +30,5 @@ public class Post {
private String text; private String text;
@CreationTimestamp @CreationTimestamp
private Instant createdAt; private Instant createdAt = Instant.now();
} }

View File

@@ -11,6 +11,7 @@ import com.usatiuk.tjv.y.server.repository.ChatRepository;
import com.usatiuk.tjv.y.server.service.exceptions.BadInputException; import com.usatiuk.tjv.y.server.service.exceptions.BadInputException;
import com.usatiuk.tjv.y.server.service.exceptions.NotFoundException; import com.usatiuk.tjv.y.server.service.exceptions.NotFoundException;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -20,6 +21,7 @@ import java.util.Collection;
import java.util.Objects; import java.util.Objects;
@Service("chatService") @Service("chatService")
@Transactional
public class ChatServiceImpl implements ChatService { public class ChatServiceImpl implements ChatService {
private final ChatRepository chatRepository; private final ChatRepository chatRepository;

View File

@@ -3,12 +3,14 @@ package com.usatiuk.tjv.y.server.service;
import com.usatiuk.tjv.y.server.dto.TokenRequestTo; import com.usatiuk.tjv.y.server.dto.TokenRequestTo;
import com.usatiuk.tjv.y.server.dto.TokenResponseTo; import com.usatiuk.tjv.y.server.dto.TokenResponseTo;
import com.usatiuk.tjv.y.server.security.JwtTokenService; import com.usatiuk.tjv.y.server.security.JwtTokenService;
import jakarta.transaction.Transactional;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@Transactional
public class LoginTokenServiceImpl implements LoginTokenService { public class LoginTokenServiceImpl implements LoginTokenService {
private final AuthenticationManager authenticationManager; private final AuthenticationManager authenticationManager;
private final JwtTokenService jwtTokenService; private final JwtTokenService jwtTokenService;

View File

@@ -9,6 +9,7 @@ import com.usatiuk.tjv.y.server.entity.Person;
import com.usatiuk.tjv.y.server.repository.MessageRepository; import com.usatiuk.tjv.y.server.repository.MessageRepository;
import com.usatiuk.tjv.y.server.service.exceptions.NotFoundException; import com.usatiuk.tjv.y.server.service.exceptions.NotFoundException;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -17,6 +18,7 @@ import java.util.Objects;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
@Service("messageService") @Service("messageService")
@Transactional
public class MessageServiceImpl implements MessageService { public class MessageServiceImpl implements MessageService {
private final MessageRepository messageRepository; private final MessageRepository messageRepository;

View File

@@ -20,6 +20,7 @@ import java.util.Optional;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
@Service @Service
@Transactional
public class PersonServiceImpl implements PersonService { public class PersonServiceImpl implements PersonService {
private final PersonRepository personRepository; private final PersonRepository personRepository;
private final PasswordEncoder passwordEncoder; private final PasswordEncoder passwordEncoder;
@@ -76,7 +77,6 @@ public class PersonServiceImpl implements PersonService {
} }
@Override @Override
@Transactional
public PersonTo update(Authentication authentication, PersonCreateTo person) { public PersonTo update(Authentication authentication, PersonCreateTo person) {
var found = personRepository.findById(authentication.getName()).orElseThrow(NotFoundException::new); var found = personRepository.findById(authentication.getName()).orElseThrow(NotFoundException::new);
@@ -90,7 +90,6 @@ public class PersonServiceImpl implements PersonService {
return personMapper.makeDto(found); return personMapper.makeDto(found);
} }
@Transactional
public void deleteByUuid(String uuid) { public void deleteByUuid(String uuid) {
var person = personRepository.findById(uuid).orElseThrow(NotFoundException::new); var person = personRepository.findById(uuid).orElseThrow(NotFoundException::new);
for (Chat c : person.getChats()) { for (Chat c : person.getChats()) {
@@ -104,7 +103,6 @@ public class PersonServiceImpl implements PersonService {
} }
@Override @Override
@Transactional
public void deleteSelf(Authentication authentication) { public void deleteSelf(Authentication authentication) {
deleteByUuid(authentication.getName()); deleteByUuid(authentication.getName());
} }

View File

@@ -8,6 +8,7 @@ import com.usatiuk.tjv.y.server.entity.Post;
import com.usatiuk.tjv.y.server.repository.PostRepository; import com.usatiuk.tjv.y.server.repository.PostRepository;
import com.usatiuk.tjv.y.server.service.exceptions.NotFoundException; import com.usatiuk.tjv.y.server.service.exceptions.NotFoundException;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -16,6 +17,7 @@ import java.util.Objects;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
@Service("postService") @Service("postService")
@Transactional
public class PostServiceImpl implements PostService { public class PostServiceImpl implements PostService {
private final PostRepository postRepository; private final PostRepository postRepository;
private final PostMapper postMapper; private final PostMapper postMapper;

View File

@@ -1,5 +1,6 @@
package com.usatiuk.tjv.y.server.spasupport; package com.usatiuk.tjv.y.server.spasupport;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.Profile;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -14,6 +15,7 @@ import java.nio.file.Files;
@RestController @RestController
@RequestMapping(value = "/app", produces = MediaType.TEXT_HTML_VALUE) @RequestMapping(value = "/app", produces = MediaType.TEXT_HTML_VALUE)
@Profile("prod") @Profile("prod")
@Hidden
class AppRootContoller { class AppRootContoller {
private final File indexFile; private final File indexFile;