Compare commits
No commits in common. "c25cd4435d03e1eb401f384daacac831349a186e" and "aa63fc66b8680aafa2688d7898ffa2fa3ba75022" have entirely different histories.
c25cd4435d
...
aa63fc66b8
2
.env
2
.env
@ -6,4 +6,4 @@ MINIO_ENDPOINT=http://omv.badger-pirarucu.ts.net:9000
|
|||||||
MINIO_USER=admin
|
MINIO_USER=admin
|
||||||
MINIO_PASS=!E9v4i0v3
|
MINIO_PASS=!E9v4i0v3
|
||||||
|
|
||||||
WEBSCRAPPER_ENDPOINT=http://localhost:8090/url
|
WEBSCRAPPER_ENDPOINT=http://localhost:8000/url
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,7 +8,6 @@ target/
|
|||||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||||
|
|
||||||
# User-specific stuff
|
# User-specific stuff
|
||||||
.idea/**
|
|
||||||
.idea/**/workspace.xml
|
.idea/**/workspace.xml
|
||||||
.idea/**/tasks.xml
|
.idea/**/tasks.xml
|
||||||
.idea/**/usage.statistics.xml
|
.idea/**/usage.statistics.xml
|
||||||
|
|||||||
@ -3,8 +3,8 @@ package com.magamochi.mangamochi.config;
|
|||||||
import com.magamochi.mangamochi.security.JwtRequestFilter;
|
import com.magamochi.mangamochi.security.JwtRequestFilter;
|
||||||
import com.magamochi.mangamochi.service.CustomUserDetailsService;
|
import com.magamochi.mangamochi.service.CustomUserDetailsService;
|
||||||
import com.magamochi.mangamochi.util.JwtUtil;
|
import com.magamochi.mangamochi.util.JwtUtil;
|
||||||
import java.util.List;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
@ -21,62 +21,71 @@ import org.springframework.web.cors.CorsConfiguration;
|
|||||||
import org.springframework.web.cors.CorsConfigurationSource;
|
import org.springframework.web.cors.CorsConfigurationSource;
|
||||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableMethodSecurity
|
@EnableMethodSecurity
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class SecurityConfig {
|
public class SecurityConfig {
|
||||||
|
|
||||||
private final CustomUserDetailsService userDetailsService;
|
private final CustomUserDetailsService userDetailsService;
|
||||||
private final JwtUtil jwtUtil;
|
private final JwtUtil jwtUtil;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public JwtRequestFilter jwtRequestFilter() {
|
public JwtRequestFilter jwtRequestFilter() {
|
||||||
return new JwtRequestFilter(jwtUtil, userDetailsService);
|
return new JwtRequestFilter(jwtUtil, userDetailsService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DaoAuthenticationProvider authenticationProvider() {
|
public DaoAuthenticationProvider authenticationProvider() {
|
||||||
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
|
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
|
||||||
authProvider.setUserDetailsService(userDetailsService);
|
authProvider.setUserDetailsService(userDetailsService);
|
||||||
authProvider.setPasswordEncoder(passwordEncoder());
|
authProvider.setPasswordEncoder(passwordEncoder());
|
||||||
return authProvider;
|
return authProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig)
|
public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
|
||||||
throws Exception {
|
return authConfig.getAuthenticationManager();
|
||||||
return authConfig.getAuthenticationManager();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public PasswordEncoder passwordEncoder() {
|
public PasswordEncoder passwordEncoder() {
|
||||||
return new BCryptPasswordEncoder();
|
return new BCryptPasswordEncoder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
http.cors(cors -> cors.configurationSource(corsConfigurationSource()))
|
http
|
||||||
.csrf(csrf -> csrf.disable())
|
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
|
||||||
.sessionManagement(
|
.csrf(csrf -> csrf.disable())
|
||||||
session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||||
.authorizeHttpRequests(auth -> auth.anyRequest().permitAll())
|
.authorizeHttpRequests(auth -> auth
|
||||||
.headers(headers -> headers.frameOptions(frame -> frame.disable()))
|
.requestMatchers("/auth/**").permitAll()
|
||||||
.authenticationProvider(authenticationProvider())
|
.requestMatchers("/swagger-ui/**").permitAll()
|
||||||
.addFilterBefore(jwtRequestFilter(), UsernamePasswordAuthenticationFilter.class);
|
.requestMatchers("/api-docs/**").permitAll()
|
||||||
|
.requestMatchers("/h2-console/**").permitAll()
|
||||||
|
.anyRequest().permitAll()
|
||||||
|
)
|
||||||
|
.headers(headers -> headers
|
||||||
|
.frameOptions(frame -> frame.disable())
|
||||||
|
)
|
||||||
|
.authenticationProvider(authenticationProvider())
|
||||||
|
.addFilterBefore(jwtRequestFilter(), UsernamePasswordAuthenticationFilter.class);
|
||||||
|
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public CorsConfigurationSource corsConfigurationSource() {
|
public CorsConfigurationSource corsConfigurationSource() {
|
||||||
CorsConfiguration configuration = new CorsConfiguration();
|
CorsConfiguration configuration = new CorsConfiguration();
|
||||||
configuration.setAllowedOriginPatterns(List.of("http://localhost:3000"));
|
configuration.setAllowedOriginPatterns(List.of("http://localhost:3000"));
|
||||||
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
|
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
|
||||||
configuration.setAllowedHeaders(List.of("Authorization", "Content-Type", "X-Requested-With"));
|
configuration.setAllowedHeaders(List.of("Authorization", "Content-Type", "X-Requested-With"));
|
||||||
configuration.setAllowCredentials(true);
|
configuration.setAllowCredentials(true);
|
||||||
|
|
||||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||||
source.registerCorsConfiguration("/**", configuration);
|
source.registerCorsConfiguration("/**", configuration);
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2,11 +2,15 @@ package com.magamochi.mangamochi.controller;
|
|||||||
|
|
||||||
import com.magamochi.mangamochi.model.dto.AuthenticationRequestDTO;
|
import com.magamochi.mangamochi.model.dto.AuthenticationRequestDTO;
|
||||||
import com.magamochi.mangamochi.model.dto.AuthenticationResponseDTO;
|
import com.magamochi.mangamochi.model.dto.AuthenticationResponseDTO;
|
||||||
import com.magamochi.mangamochi.model.dto.RegistrationRequestDTO;
|
import com.magamochi.mangamochi.model.entity.User;
|
||||||
import com.magamochi.mangamochi.model.repository.UserRepository;
|
import com.magamochi.mangamochi.model.repository.UserRepository;
|
||||||
import com.magamochi.mangamochi.service.UserService;
|
import com.magamochi.mangamochi.util.JwtUtil;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@ -15,28 +19,46 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
@CrossOrigin(origins = "*")
|
@CrossOrigin(origins = "*")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class AuthenticationController {
|
public class AuthenticationController {
|
||||||
private final UserService userService;
|
private final AuthenticationManager authenticationManager;
|
||||||
|
private final UserDetailsService userDetailsService;
|
||||||
|
private final JwtUtil jwtUtil;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
@Operation(
|
|
||||||
summary = "Authenticate user",
|
|
||||||
description = "Authenticate user with email and password.",
|
|
||||||
tags = {"Auth"},
|
|
||||||
operationId = "authenticateUser")
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public AuthenticationResponseDTO authenticateUser(
|
public ResponseEntity<?> createAuthenticationToken(
|
||||||
@RequestBody AuthenticationRequestDTO authenticationRequestDTO) {
|
@RequestBody AuthenticationRequestDTO authenticationRequestDTO) {
|
||||||
return userService.authenticate(authenticationRequestDTO);
|
try {
|
||||||
|
authenticationManager.authenticate(
|
||||||
|
new UsernamePasswordAuthenticationToken(
|
||||||
|
authenticationRequestDTO.username(), authenticationRequestDTO.password()));
|
||||||
|
} catch (BadCredentialsException e) {
|
||||||
|
return ResponseEntity.badRequest().body("Incorrect username or password");
|
||||||
|
}
|
||||||
|
|
||||||
|
final var userDetails =
|
||||||
|
userDetailsService.loadUserByUsername(authenticationRequestDTO.username());
|
||||||
|
final var jwt = jwtUtil.generateToken(userDetails);
|
||||||
|
|
||||||
|
var user = userRepository.findByUsername(userDetails.getUsername()).orElseThrow();
|
||||||
|
var role = user.getRole();
|
||||||
|
|
||||||
|
return ResponseEntity.ok(new AuthenticationResponseDTO(jwt, userDetails.getUsername(), role));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
|
||||||
summary = "Register user",
|
|
||||||
description = "Register a new user.",
|
|
||||||
tags = {"Auth"},
|
|
||||||
operationId = "registerUser")
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public void registerUser(@RequestBody RegistrationRequestDTO registrationRequestDTO) {
|
public ResponseEntity<?> registerUser(
|
||||||
userService.register(registrationRequestDTO);
|
@RequestBody AuthenticationRequestDTO registrationRequestDTO) {
|
||||||
|
if (userRepository.existsByUsername(registrationRequestDTO.username())) {
|
||||||
|
return ResponseEntity.badRequest().body("Username is already taken");
|
||||||
|
}
|
||||||
|
|
||||||
|
userRepository.save(
|
||||||
|
User.builder()
|
||||||
|
.username(registrationRequestDTO.username())
|
||||||
|
.password(passwordEncoder.encode(registrationRequestDTO.password()))
|
||||||
|
.build());
|
||||||
|
|
||||||
|
return ResponseEntity.ok("User registered successfully");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
package com.magamochi.mangamochi.controller;
|
package com.magamochi.mangamochi.controller;
|
||||||
|
|
||||||
import com.magamochi.mangamochi.model.dto.*;
|
import com.magamochi.mangamochi.model.dto.MangaChapterDTO;
|
||||||
|
import com.magamochi.mangamochi.model.dto.MangaChapterImagesDTO;
|
||||||
|
import com.magamochi.mangamochi.model.dto.MangaDTO;
|
||||||
|
import com.magamochi.mangamochi.model.dto.MangaListDTO;
|
||||||
import com.magamochi.mangamochi.model.enumeration.ArchiveFileType;
|
import com.magamochi.mangamochi.model.enumeration.ArchiveFileType;
|
||||||
|
import com.magamochi.mangamochi.model.specification.MangaSpecification;
|
||||||
import com.magamochi.mangamochi.service.MangaService;
|
import com.magamochi.mangamochi.service.MangaService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
@ -33,9 +37,9 @@ public class MangaController {
|
|||||||
operationId = "getMangas")
|
operationId = "getMangas")
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public Page<MangaListDTO> getMangas(
|
public Page<MangaListDTO> getMangas(
|
||||||
@ParameterObject MangaListFilterDTO filterDTO,
|
@ParameterObject MangaSpecification specification,
|
||||||
@Parameter(hidden = true) @ParameterObject @PageableDefault Pageable pageable) {
|
@Parameter(hidden = true) @ParameterObject @PageableDefault Pageable pageable) {
|
||||||
return mangaService.getMangas(filterDTO, pageable);
|
return mangaService.getMangas(specification, pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
|
|||||||
@ -1,33 +0,0 @@
|
|||||||
package com.magamochi.mangamochi.controller;
|
|
||||||
|
|
||||||
import com.magamochi.mangamochi.service.UserFavoriteMangaService;
|
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/mangas")
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class UserFavoriteMangaController {
|
|
||||||
private final UserFavoriteMangaService userFavoriteMangaService;
|
|
||||||
|
|
||||||
@Operation(
|
|
||||||
summary = "Favorite a manga",
|
|
||||||
description = "Set a manga as favorite for the logged user.",
|
|
||||||
tags = {"Favorite Mangas"},
|
|
||||||
operationId = "setFavorite")
|
|
||||||
@PostMapping("/{id}/favorite")
|
|
||||||
public void setFavorite(@PathVariable Long id) {
|
|
||||||
userFavoriteMangaService.setFavorite(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Operation(
|
|
||||||
summary = "Unfavorite a manga",
|
|
||||||
description = "Remove a manga from favorites for the logged user.",
|
|
||||||
tags = {"Favorite Mangas"},
|
|
||||||
operationId = "setUnfavorite")
|
|
||||||
@PostMapping("/{id}/unfavorite")
|
|
||||||
public void setUnfavorite(@PathVariable Long id) {
|
|
||||||
userFavoriteMangaService.setUnfavorite(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +1,3 @@
|
|||||||
package com.magamochi.mangamochi.model.dto;
|
package com.magamochi.mangamochi.model.dto;
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
public record AuthenticationRequestDTO(String username, String password) {}
|
||||||
|
|
||||||
public record AuthenticationRequestDTO(@NotNull String email, @NotNull String password) {}
|
|
||||||
|
|||||||
@ -1,11 +1,3 @@
|
|||||||
package com.magamochi.mangamochi.model.dto;
|
package com.magamochi.mangamochi.model.dto;
|
||||||
|
|
||||||
import com.magamochi.mangamochi.model.enumeration.UserRole;
|
public record AuthenticationResponseDTO(String token, String username, String role) {}
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
public record AuthenticationResponseDTO(
|
|
||||||
@NotNull Long id,
|
|
||||||
@NotNull String token,
|
|
||||||
@NotNull String email,
|
|
||||||
@NotNull String name,
|
|
||||||
@NotNull UserRole role) {}
|
|
||||||
|
|||||||
@ -18,9 +18,8 @@ public record MangaListDTO(
|
|||||||
Integer providerCount,
|
Integer providerCount,
|
||||||
@NotNull List<String> genres,
|
@NotNull List<String> genres,
|
||||||
@NotNull List<String> authors,
|
@NotNull List<String> authors,
|
||||||
@NotNull Double score,
|
@NotNull Double score) {
|
||||||
@NotNull Boolean favorite) {
|
public static MangaListDTO from(Manga manga) {
|
||||||
public static MangaListDTO from(Manga manga, boolean favorite) {
|
|
||||||
return new MangaListDTO(
|
return new MangaListDTO(
|
||||||
manga.getId(),
|
manga.getId(),
|
||||||
manga.getTitle(),
|
manga.getTitle(),
|
||||||
@ -33,7 +32,6 @@ public record MangaListDTO(
|
|||||||
manga.getMangaAuthors().stream()
|
manga.getMangaAuthors().stream()
|
||||||
.map(mangaAuthor -> mangaAuthor.getAuthor().getName())
|
.map(mangaAuthor -> mangaAuthor.getAuthor().getName())
|
||||||
.toList(),
|
.toList(),
|
||||||
0.0,
|
0.0);
|
||||||
favorite);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +0,0 @@
|
|||||||
package com.magamochi.mangamochi.model.dto;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public record MangaListFilterDTO(
|
|
||||||
String searchQuery, List<Long> genreIds, List<String> statuses, Boolean userFavorites) {}
|
|
||||||
@ -1,3 +0,0 @@
|
|||||||
package com.magamochi.mangamochi.model.dto;
|
|
||||||
|
|
||||||
public record RegistrationRequestDTO(String name, String email, String password) {}
|
|
||||||
@ -57,7 +57,4 @@ public class Manga {
|
|||||||
|
|
||||||
@OneToMany(mappedBy = "manga")
|
@OneToMany(mappedBy = "manga")
|
||||||
private List<MangaGenre> mangaGenres;
|
private List<MangaGenre> mangaGenres;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "manga")
|
|
||||||
private List<UserFavoriteManga> userFavoriteMangas;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
package com.magamochi.mangamochi.model.entity;
|
package com.magamochi.mangamochi.model.entity;
|
||||||
|
|
||||||
import com.magamochi.mangamochi.model.enumeration.UserRole;
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import java.util.Set;
|
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@ -18,18 +16,11 @@ public class User {
|
|||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Column(unique = true, nullable = false)
|
@Column(unique = true, nullable = false)
|
||||||
private String email;
|
private String username;
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@Enumerated(EnumType.STRING)
|
private String role;
|
||||||
private UserRole role;
|
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user")
|
|
||||||
private Set<UserFavoriteManga> favoriteMangas;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,29 +0,0 @@
|
|||||||
package com.magamochi.mangamochi.model.entity;
|
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
import java.time.Instant;
|
|
||||||
import lombok.*;
|
|
||||||
import org.hibernate.annotations.CreationTimestamp;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
@Table(name = "user_favorite_mangas")
|
|
||||||
@Builder
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
public class UserFavoriteManga {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@ManyToOne
|
|
||||||
@JoinColumn(name = "user_id")
|
|
||||||
private User user;
|
|
||||||
|
|
||||||
@ManyToOne
|
|
||||||
@JoinColumn(name = "manga_id")
|
|
||||||
private Manga manga;
|
|
||||||
|
|
||||||
@CreationTimestamp private Instant createdAt;
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
package com.magamochi.mangamochi.model.enumeration;
|
|
||||||
|
|
||||||
public enum UserRole {
|
|
||||||
USER
|
|
||||||
}
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
package com.magamochi.mangamochi.model.repository;
|
|
||||||
|
|
||||||
import com.magamochi.mangamochi.model.entity.Manga;
|
|
||||||
import com.magamochi.mangamochi.model.entity.User;
|
|
||||||
import com.magamochi.mangamochi.model.entity.UserFavoriteManga;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
public interface UserFavoriteMangaRepository extends JpaRepository<UserFavoriteManga, Long> {
|
|
||||||
boolean existsByUserAndManga(User user, Manga manga);
|
|
||||||
|
|
||||||
Optional<UserFavoriteManga> findByUserAndManga(User user, Manga manga);
|
|
||||||
|
|
||||||
Set<UserFavoriteManga> findByUser(User user);
|
|
||||||
}
|
|
||||||
@ -5,7 +5,7 @@ import java.util.Optional;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface UserRepository extends JpaRepository<User, Long> {
|
public interface UserRepository extends JpaRepository<User, Long> {
|
||||||
boolean existsByEmail(String email);
|
Optional<User> findByUsername(String username);
|
||||||
|
|
||||||
Optional<User> findByEmail(String email);
|
boolean existsByUsername(String username);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,65 +2,53 @@ package com.magamochi.mangamochi.model.specification;
|
|||||||
|
|
||||||
import static java.util.Objects.nonNull;
|
import static java.util.Objects.nonNull;
|
||||||
|
|
||||||
import com.magamochi.mangamochi.model.dto.MangaListFilterDTO;
|
|
||||||
import com.magamochi.mangamochi.model.entity.Author;
|
import com.magamochi.mangamochi.model.entity.Author;
|
||||||
import com.magamochi.mangamochi.model.entity.Manga;
|
import com.magamochi.mangamochi.model.entity.Manga;
|
||||||
import com.magamochi.mangamochi.model.entity.User;
|
|
||||||
import jakarta.persistence.criteria.*;
|
import jakarta.persistence.criteria.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import lombok.AccessLevel;
|
import lombok.NonNull;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
|
||||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
public record MangaSpecification(String searchQuery, List<Long> genreIds, List<String> statuses)
|
||||||
public class MangaSpecification {
|
implements Specification<Manga> {
|
||||||
public static Specification<Manga> getMangaListSpecification(
|
@Override
|
||||||
MangaListFilterDTO filterDTO, User loggedUser) {
|
public Predicate toPredicate(
|
||||||
return (root, query, criteriaBuilder) -> {
|
@NonNull Root<Manga> root, CriteriaQuery<?> query, @NonNull CriteriaBuilder criteriaBuilder) {
|
||||||
List<Predicate> predicates = new ArrayList<>();
|
List<Predicate> predicates = new ArrayList<>();
|
||||||
|
|
||||||
if (StringUtils.isNotBlank(filterDTO.searchQuery())) {
|
if (StringUtils.isNotBlank(searchQuery)) {
|
||||||
var searchPattern = "%" + filterDTO.searchQuery().toLowerCase() + "%";
|
var searchPattern = "%" + searchQuery.toLowerCase() + "%";
|
||||||
|
|
||||||
var titlePredicate =
|
var titlePredicate =
|
||||||
criteriaBuilder.like(criteriaBuilder.lower(root.get("title")), searchPattern);
|
criteriaBuilder.like(criteriaBuilder.lower(root.get("title")), searchPattern);
|
||||||
|
|
||||||
var authorsPredicate = searchInAuthors(root, query, criteriaBuilder, searchPattern);
|
var authorsPredicate = searchInAuthors(root, query, criteriaBuilder, searchPattern);
|
||||||
|
|
||||||
var searchPredicate = criteriaBuilder.or(titlePredicate, authorsPredicate);
|
var searchPredicate = criteriaBuilder.or(titlePredicate, authorsPredicate);
|
||||||
|
|
||||||
predicates.add(searchPredicate);
|
predicates.add(searchPredicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nonNull(filterDTO.genreIds()) && !filterDTO.genreIds().isEmpty()) {
|
if (nonNull(genreIds) && !genreIds.isEmpty()) {
|
||||||
var genreJoin = root.join("mangaGenres", JoinType.LEFT);
|
var genreJoin = root.join("mangaGenres", JoinType.LEFT);
|
||||||
predicates.add(genreJoin.get("genre").get("id").in(filterDTO.genreIds()));
|
predicates.add(genreJoin.get("genre").get("id").in(genreIds));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nonNull(filterDTO.statuses()) && !filterDTO.statuses().isEmpty()) {
|
if (nonNull(statuses) && !statuses.isEmpty()) {
|
||||||
predicates.add(
|
predicates.add(
|
||||||
criteriaBuilder
|
criteriaBuilder
|
||||||
.lower(root.get("status"))
|
.lower(root.get("status"))
|
||||||
.in(filterDTO.statuses().stream().map(String::toLowerCase).toList()));
|
.in(statuses.stream().map(String::toLowerCase).toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nonNull(filterDTO.userFavorites()) && filterDTO.userFavorites().equals(Boolean.TRUE)) {
|
query.distinct(true);
|
||||||
if (nonNull(loggedUser)) {
|
|
||||||
var userFavoritesJoin = root.join("userFavoriteMangas", JoinType.INNER);
|
|
||||||
predicates.add(
|
|
||||||
criteriaBuilder.equal(userFavoritesJoin.get("user").get("id"), loggedUser.getId()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
query.distinct(true);
|
return criteriaBuilder.and(predicates.toArray(Predicate[]::new));
|
||||||
|
|
||||||
return criteriaBuilder.and(predicates.toArray(Predicate[]::new));
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Predicate searchInAuthors(
|
private Predicate searchInAuthors(
|
||||||
Root<Manga> root,
|
Root<Manga> root,
|
||||||
CriteriaQuery<?> query,
|
CriteriaQuery<?> query,
|
||||||
CriteriaBuilder criteriaBuilder,
|
CriteriaBuilder criteriaBuilder,
|
||||||
|
|||||||
@ -6,8 +6,8 @@ import jakarta.servlet.FilterChain;
|
|||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
@ -15,43 +15,43 @@ import org.springframework.security.web.authentication.WebAuthenticationDetailsS
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class JwtRequestFilter extends OncePerRequestFilter {
|
public class JwtRequestFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
private final JwtUtil jwtUtil;
|
private final JwtUtil jwtUtil;
|
||||||
private final CustomUserDetailsService userDetailsService;
|
private final CustomUserDetailsService userDetailsService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||||
HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
FilterChain chain) throws ServletException, IOException {
|
||||||
throws ServletException, IOException {
|
|
||||||
|
|
||||||
final String authorizationHeader = request.getHeader("Authorization");
|
final String authorizationHeader = request.getHeader("Authorization");
|
||||||
|
|
||||||
String username = null;
|
String username = null;
|
||||||
String jwt = null;
|
String jwt = null;
|
||||||
|
|
||||||
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
|
||||||
jwt = authorizationHeader.substring(7);
|
jwt = authorizationHeader.substring(7);
|
||||||
try {
|
try {
|
||||||
username = jwtUtil.extractUsername(jwt);
|
username = jwtUtil.extractUsername(jwt);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.warn("JWT token validation failed", e);
|
logger.warn("JWT token validation failed", e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
|
||||||
|
UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
|
||||||
|
|
||||||
|
if (jwtUtil.validateToken(jwt, userDetails)) {
|
||||||
|
UsernamePasswordAuthenticationToken authToken =
|
||||||
|
new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
|
||||||
|
authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
||||||
|
SecurityContextHolder.getContext().setAuthentication(authToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chain.doFilter(request, response);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
|
|
||||||
UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);
|
|
||||||
|
|
||||||
if (jwtUtil.validateToken(jwt, userDetails)) {
|
|
||||||
UsernamePasswordAuthenticationToken authToken =
|
|
||||||
new UsernamePasswordAuthenticationToken(
|
|
||||||
userDetails, null, userDetails.getAuthorities());
|
|
||||||
authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
|
||||||
SecurityContextHolder.getContext().setAuthentication(authToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
chain.doFilter(request, response);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -19,12 +19,12 @@ public class CustomUserDetailsService implements UserDetailsService {
|
|||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
var user =
|
var user =
|
||||||
userRepository
|
userRepository
|
||||||
.findByEmail(username)
|
.findByUsername(username)
|
||||||
.orElseThrow(
|
.orElseThrow(
|
||||||
() -> new UsernameNotFoundException("User not found with email: " + username));
|
() -> new UsernameNotFoundException("User not found with username: " + username));
|
||||||
|
|
||||||
return new User(
|
return new User(
|
||||||
user.getEmail(),
|
user.getUsername(),
|
||||||
user.getPassword(),
|
user.getPassword(),
|
||||||
Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + user.getRole())));
|
Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + user.getRole())));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,15 @@
|
|||||||
package com.magamochi.mangamochi.service;
|
package com.magamochi.mangamochi.service;
|
||||||
|
|
||||||
import static java.util.Objects.nonNull;
|
|
||||||
|
|
||||||
import com.magamochi.mangamochi.client.JikanClient;
|
import com.magamochi.mangamochi.client.JikanClient;
|
||||||
import com.magamochi.mangamochi.model.dto.*;
|
import com.magamochi.mangamochi.model.dto.*;
|
||||||
import com.magamochi.mangamochi.model.entity.Manga;
|
|
||||||
import com.magamochi.mangamochi.model.entity.MangaChapter;
|
import com.magamochi.mangamochi.model.entity.MangaChapter;
|
||||||
import com.magamochi.mangamochi.model.entity.MangaChapterImage;
|
import com.magamochi.mangamochi.model.entity.MangaChapterImage;
|
||||||
import com.magamochi.mangamochi.model.entity.MangaProvider;
|
import com.magamochi.mangamochi.model.entity.MangaProvider;
|
||||||
import com.magamochi.mangamochi.model.enumeration.ArchiveFileType;
|
import com.magamochi.mangamochi.model.enumeration.ArchiveFileType;
|
||||||
import com.magamochi.mangamochi.model.repository.*;
|
import com.magamochi.mangamochi.model.repository.MangaChapterImageRepository;
|
||||||
|
import com.magamochi.mangamochi.model.repository.MangaChapterRepository;
|
||||||
|
import com.magamochi.mangamochi.model.repository.MangaProviderRepository;
|
||||||
|
import com.magamochi.mangamochi.model.repository.MangaRepository;
|
||||||
import com.magamochi.mangamochi.model.specification.MangaSpecification;
|
import com.magamochi.mangamochi.model.specification.MangaSpecification;
|
||||||
import com.magamochi.mangamochi.service.providers.ContentProviderFactory;
|
import com.magamochi.mangamochi.service.providers.ContentProviderFactory;
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
@ -20,8 +20,6 @@ import java.net.*;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@ -33,7 +31,6 @@ import org.springframework.stereotype.Service;
|
|||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class MangaService {
|
public class MangaService {
|
||||||
private final UserService userService;
|
|
||||||
private final MangaChapterRepository mangaChapterRepository;
|
private final MangaChapterRepository mangaChapterRepository;
|
||||||
private final MangaRepository mangaRepository;
|
private final MangaRepository mangaRepository;
|
||||||
private final MangaProviderRepository mangaProviderRepository;
|
private final MangaProviderRepository mangaProviderRepository;
|
||||||
@ -43,27 +40,9 @@ public class MangaService {
|
|||||||
private final JikanClient jikanClient;
|
private final JikanClient jikanClient;
|
||||||
|
|
||||||
private final ContentProviderFactory contentProviderFactory;
|
private final ContentProviderFactory contentProviderFactory;
|
||||||
private final UserFavoriteMangaRepository userFavoriteMangaRepository;
|
|
||||||
|
|
||||||
public Page<MangaListDTO> getMangas(MangaListFilterDTO filterDTO, Pageable pageable) {
|
public Page<MangaListDTO> getMangas(MangaSpecification specification, Pageable pageable) {
|
||||||
var user = userService.getLoggedUser();
|
return mangaRepository.findAll(specification, pageable).map(MangaListDTO::from);
|
||||||
|
|
||||||
var specification = MangaSpecification.getMangaListSpecification(filterDTO, user);
|
|
||||||
|
|
||||||
var favoriteMangasIds =
|
|
||||||
nonNull(user)
|
|
||||||
? userFavoriteMangaRepository.findByUser(user).stream()
|
|
||||||
.map(ufm -> ufm.getManga().getId())
|
|
||||||
.collect(Collectors.toSet())
|
|
||||||
: Set.of();
|
|
||||||
|
|
||||||
return mangaRepository
|
|
||||||
.findAll(specification, pageable)
|
|
||||||
.map(
|
|
||||||
manga -> {
|
|
||||||
var favorite = favoriteMangasIds.contains(manga.getId());
|
|
||||||
return MangaListDTO.from(manga, favorite);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<MangaChapterDTO> getMangaChapters(Long mangaProviderId) {
|
public List<MangaChapterDTO> getMangaChapters(Long mangaProviderId) {
|
||||||
@ -199,7 +178,10 @@ public class MangaService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateInfo(Long mangaId) {
|
public void updateInfo(Long mangaId) {
|
||||||
var manga = findMangaByIdThrowIfNotFound(mangaId);
|
var manga =
|
||||||
|
mangaRepository
|
||||||
|
.findById(mangaId)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Manga not found"));
|
||||||
|
|
||||||
var mangaSearchResponse = jikanClient.mangaSearch(manga.getTitle());
|
var mangaSearchResponse = jikanClient.mangaSearch(manga.getTitle());
|
||||||
if (mangaSearchResponse.data().isEmpty()) {
|
if (mangaSearchResponse.data().isEmpty()) {
|
||||||
@ -215,7 +197,10 @@ public class MangaService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public MangaDTO getManga(Long mangaId) {
|
public MangaDTO getManga(Long mangaId) {
|
||||||
var manga = findMangaByIdThrowIfNotFound(mangaId);
|
var manga =
|
||||||
|
mangaRepository
|
||||||
|
.findById(mangaId)
|
||||||
|
.orElseThrow(() -> new RuntimeException("Manga not found"));
|
||||||
|
|
||||||
return MangaDTO.from(manga);
|
return MangaDTO.from(manga);
|
||||||
}
|
}
|
||||||
@ -251,10 +236,4 @@ public class MangaService {
|
|||||||
|
|
||||||
mangaChapterRepository.save(chapter);
|
mangaChapterRepository.save(chapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Manga findMangaByIdThrowIfNotFound(Long mangaId) {
|
|
||||||
return mangaRepository
|
|
||||||
.findById(mangaId)
|
|
||||||
.orElseThrow(() -> new RuntimeException("Manga not found"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,46 +0,0 @@
|
|||||||
package com.magamochi.mangamochi.service;
|
|
||||||
|
|
||||||
import com.magamochi.mangamochi.model.entity.Manga;
|
|
||||||
import com.magamochi.mangamochi.model.entity.UserFavoriteManga;
|
|
||||||
import com.magamochi.mangamochi.model.repository.MangaRepository;
|
|
||||||
import com.magamochi.mangamochi.model.repository.UserFavoriteMangaRepository;
|
|
||||||
import java.util.NoSuchElementException;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class UserFavoriteMangaService {
|
|
||||||
private final UserService userService;
|
|
||||||
private final MangaRepository mangaRepository;
|
|
||||||
private final UserFavoriteMangaRepository userFavoriteMangaRepository;
|
|
||||||
|
|
||||||
public void setFavorite(Long id) {
|
|
||||||
var user = userService.getLoggedUserThrowIfNotFound();
|
|
||||||
var manga = findMangaByIdThrowIfNotFound(id);
|
|
||||||
|
|
||||||
if (userFavoriteMangaRepository.existsByUserAndManga(user, manga)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
userFavoriteMangaRepository.save(UserFavoriteManga.builder().user(user).manga(manga).build());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUnfavorite(Long id) {
|
|
||||||
var user = userService.getLoggedUserThrowIfNotFound();
|
|
||||||
var manga = findMangaByIdThrowIfNotFound(id);
|
|
||||||
|
|
||||||
var favoriteManga =
|
|
||||||
userFavoriteMangaRepository
|
|
||||||
.findByUserAndManga(user, manga)
|
|
||||||
.orElseThrow(() -> new NoSuchElementException("No manga found"));
|
|
||||||
|
|
||||||
userFavoriteMangaRepository.delete(favoriteManga);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Manga findMangaByIdThrowIfNotFound(Long mangaId) {
|
|
||||||
return mangaRepository
|
|
||||||
.findById(mangaId)
|
|
||||||
.orElseThrow(() -> new RuntimeException("Manga not found"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,84 +0,0 @@
|
|||||||
package com.magamochi.mangamochi.service;
|
|
||||||
|
|
||||||
import static java.util.Objects.isNull;
|
|
||||||
|
|
||||||
import com.magamochi.mangamochi.model.dto.AuthenticationRequestDTO;
|
|
||||||
import com.magamochi.mangamochi.model.dto.AuthenticationResponseDTO;
|
|
||||||
import com.magamochi.mangamochi.model.dto.RegistrationRequestDTO;
|
|
||||||
import com.magamochi.mangamochi.model.entity.User;
|
|
||||||
import com.magamochi.mangamochi.model.enumeration.UserRole;
|
|
||||||
import com.magamochi.mangamochi.model.repository.UserRepository;
|
|
||||||
import com.magamochi.mangamochi.util.JwtUtil;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class UserService {
|
|
||||||
private final AuthenticationManager authenticationManager;
|
|
||||||
private final UserDetailsService userDetailsService;
|
|
||||||
private final JwtUtil jwtUtil;
|
|
||||||
private final UserRepository userRepository;
|
|
||||||
private final PasswordEncoder passwordEncoder;
|
|
||||||
|
|
||||||
public AuthenticationResponseDTO authenticate(AuthenticationRequestDTO request) {
|
|
||||||
var token = new UsernamePasswordAuthenticationToken(request.email(), request.password());
|
|
||||||
|
|
||||||
try {
|
|
||||||
authenticationManager.authenticate(token);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException("Authentication failed", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
var userDetails = userDetailsService.loadUserByUsername(request.email());
|
|
||||||
var jwt = jwtUtil.generateToken(userDetails);
|
|
||||||
|
|
||||||
var user = findUserByEmailThrowIfNotFound(userDetails.getUsername());
|
|
||||||
|
|
||||||
return new AuthenticationResponseDTO(
|
|
||||||
user.getId(), jwt, userDetails.getUsername(), user.getName(), user.getRole());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void register(RegistrationRequestDTO request) {
|
|
||||||
if (userRepository.existsByEmail(request.email())) {
|
|
||||||
throw new RuntimeException("Email is already taken");
|
|
||||||
}
|
|
||||||
|
|
||||||
userRepository.save(
|
|
||||||
User.builder()
|
|
||||||
.name(request.name())
|
|
||||||
.email(request.email())
|
|
||||||
.password(passwordEncoder.encode(request.password()))
|
|
||||||
.role(UserRole.USER)
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
|
|
||||||
public User getLoggedUserThrowIfNotFound() {
|
|
||||||
var authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
||||||
if (isNull(authentication) || authentication.getName().equals("anonymousUser")) {
|
|
||||||
throw new RuntimeException("No authenticated user found");
|
|
||||||
}
|
|
||||||
|
|
||||||
return findUserByEmailThrowIfNotFound(authentication.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public User getLoggedUser() {
|
|
||||||
var authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
||||||
if (isNull(authentication) || authentication.getName().equals("anonymousUser")) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return userRepository.findByEmail(authentication.getName()).orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private User findUserByEmailThrowIfNotFound(String email) {
|
|
||||||
return userRepository
|
|
||||||
.findByEmail(email)
|
|
||||||
.orElseThrow(() -> new RuntimeException("User not found"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -14,7 +14,6 @@ import java.util.stream.Collectors;
|
|||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.jsoup.nodes.Element;
|
import org.jsoup.nodes.Element;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@ -81,20 +80,10 @@ public class MangaLivreBlogProvider implements ContentProvider {
|
|||||||
chapterImageContainers.stream()
|
chapterImageContainers.stream()
|
||||||
.map(
|
.map(
|
||||||
chapterImageContainerElement -> {
|
chapterImageContainerElement -> {
|
||||||
|
var pageNumber = chapterImageContainerElement.id();
|
||||||
var imageElement =
|
var imageElement =
|
||||||
chapterImageContainerElement.getElementsByTag("img").getFirst();
|
chapterImageContainerElement.getElementsByTag("img").getFirst();
|
||||||
|
return imageElement.attr("data-lazy-src");
|
||||||
var dataLazySrc = imageElement.attr("data-lazy-src");
|
|
||||||
if (StringUtils.isNoneBlank(dataLazySrc)) {
|
|
||||||
return dataLazySrc;
|
|
||||||
}
|
|
||||||
|
|
||||||
var dataSrc = imageElement.attr("src");
|
|
||||||
if (StringUtils.isNoneBlank(dataSrc)) {
|
|
||||||
return dataSrc;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NoSuchElementException("Image URL not found");
|
|
||||||
})
|
})
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
email VARCHAR NOT NULL UNIQUE,
|
username VARCHAR NOT NULL UNIQUE,
|
||||||
name VARCHAR NOT NULL,
|
|
||||||
password VARCHAR NOT NULL,
|
password VARCHAR NOT NULL,
|
||||||
role VARCHAR
|
role VARCHAR
|
||||||
);
|
);
|
||||||
@ -1,8 +0,0 @@
|
|||||||
CREATE TABLE user_favorite_mangas
|
|
||||||
(
|
|
||||||
id BIGSERIAL PRIMARY KEY,
|
|
||||||
user_id BIGINT NOT NULL,
|
|
||||||
manga_id BIGINT NOT NULL,
|
|
||||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
||||||
UNIQUE (user_id, manga_id)
|
|
||||||
);
|
|
||||||
Loading…
x
Reference in New Issue
Block a user