feat: update course entity & refactor package
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
package aplp.backend.lms.api.common.exception;
|
||||
|
||||
import aplp.backend.core.common.exception.ResourceNotFoundException;
|
||||
import aplp.backend.core.common.response.ErrorResponse;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(ResourceNotFoundException.class)
|
||||
public ResponseEntity<ErrorResponse> handleNotFound(
|
||||
ResourceNotFoundException ex
|
||||
) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.NOT_FOUND)
|
||||
.body(new ErrorResponse(
|
||||
HttpStatus.NOT_FOUND.value(),
|
||||
ex.getMessage(),
|
||||
LocalDateTime.now()
|
||||
));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<ErrorResponse> handleException(
|
||||
Exception ex
|
||||
) {
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(new ErrorResponse(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||
"Internal server error",
|
||||
LocalDateTime.now()
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package aplp.backend.lms.api.controllers;
|
||||
|
||||
import aplp.backend.lms.application.dtos.CourseReqDto;
|
||||
import aplp.backend.lms.application.dtos.CourseResDto;
|
||||
import aplp.backend.lms.application.services.CourseService;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/courses")
|
||||
@RequiredArgsConstructor
|
||||
public class CourseController {
|
||||
|
||||
private final CourseService courseService;
|
||||
|
||||
@GetMapping
|
||||
public List<CourseResDto> getAll() {
|
||||
return courseService.getAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public CourseResDto getById(@PathVariable Long id) {
|
||||
return courseService.getById(id);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public CourseResDto create(@Valid @RequestBody CourseReqDto request) {
|
||||
return courseService.create(request);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public CourseResDto update(
|
||||
@PathVariable Long id,
|
||||
@RequestBody CourseReqDto request) {
|
||||
return courseService.update(id, request);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
public void delete(@PathVariable Long id) {
|
||||
courseService.delete(id);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package aplp.backend.lms.application.dtos;
|
||||
|
||||
public record CourseReqDto(
|
||||
String title,
|
||||
String description
|
||||
) {}
|
||||
@@ -1,17 +0,0 @@
|
||||
package aplp.backend.lms.application.dtos;
|
||||
|
||||
import aplp.backend.lms.domain.entities.Course;
|
||||
|
||||
public record CourseResDto(
|
||||
Long id,
|
||||
String title,
|
||||
String description
|
||||
) {
|
||||
public static CourseResDto from(Course course) {
|
||||
return new CourseResDto(
|
||||
course.getId(),
|
||||
course.getTitle(),
|
||||
course.getDescription()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package aplp.backend.lms.application.mappers;
|
||||
|
||||
import aplp.backend.lms.application.dtos.CourseReqDto;
|
||||
import aplp.backend.lms.application.dtos.CourseResDto;
|
||||
import aplp.backend.lms.domain.entities.Course;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MappingTarget;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface CourseMapper {
|
||||
|
||||
CourseResDto toResponse(Course course);
|
||||
|
||||
Course toEntity(CourseReqDto request);
|
||||
|
||||
void updateEntity(
|
||||
CourseReqDto request,
|
||||
@MappingTarget Course course
|
||||
);
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package aplp.backend.lms.application.services;
|
||||
|
||||
import aplp.backend.core.common.exception.ResourceNotFoundException;
|
||||
import aplp.backend.lms.application.dtos.CourseReqDto;
|
||||
import aplp.backend.lms.application.dtos.CourseResDto;
|
||||
import aplp.backend.lms.application.mappers.CourseMapper;
|
||||
import aplp.backend.lms.domain.entities.Course;
|
||||
import aplp.backend.lms.domain.repositories.CourseRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class CourseService {
|
||||
|
||||
private final CourseRepository courseRepository;
|
||||
private final CourseMapper courseMapper;
|
||||
|
||||
public List<CourseResDto> getAll() {
|
||||
return courseRepository.findAll()
|
||||
.stream()
|
||||
.map(CourseResDto::from)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public CourseResDto getById(Long id) {
|
||||
Course course = courseRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Course not found"));
|
||||
|
||||
return CourseResDto.from(course);
|
||||
}
|
||||
|
||||
public CourseResDto create(CourseReqDto request) {
|
||||
Course course = courseMapper.toEntity(request);
|
||||
return courseMapper.toResponse(courseRepository.save(course));
|
||||
}
|
||||
|
||||
public CourseResDto update(Long id, CourseReqDto request) {
|
||||
Course course = courseRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Course not found"));
|
||||
|
||||
courseMapper.updateEntity(request, course);
|
||||
|
||||
return courseMapper.toResponse(courseRepository.save(course));
|
||||
}
|
||||
|
||||
public void delete(Long id) {
|
||||
courseRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package aplp.backend.lms.domain.entities;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Table(name = "courses")
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class Course {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, length = 200)
|
||||
private String title;
|
||||
|
||||
@Column(length = 1000)
|
||||
private String description;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package aplp.backend.lms.domain.repositories;
|
||||
|
||||
import aplp.backend.lms.domain.entities.Course;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface CourseRepository {
|
||||
|
||||
Optional<Course> findById(Long id);
|
||||
|
||||
List<Course> findAll();
|
||||
|
||||
Course save(Course course);
|
||||
|
||||
void deleteById(Long id);
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package aplp.backend.lms.infrastructure.config;
|
||||
|
||||
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
|
||||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
|
||||
import io.swagger.v3.oas.annotations.info.Info;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.annotations.security.SecurityScheme;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@OpenAPIDefinition(
|
||||
info = @Info(
|
||||
title = "APLP LMS API",
|
||||
version = "v1",
|
||||
description = "APLP Learning Management System API"
|
||||
),
|
||||
security = {
|
||||
@SecurityRequirement(name = "bearerAuth")
|
||||
}
|
||||
)
|
||||
@SecurityScheme(
|
||||
name = "bearerAuth",
|
||||
type = SecuritySchemeType.HTTP,
|
||||
bearerFormat = "JWT",
|
||||
scheme = "bearer"
|
||||
)
|
||||
public class OpenApiConfig {
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package aplp.backend.lms.infrastructure.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(
|
||||
HttpSecurity http
|
||||
) throws Exception {
|
||||
|
||||
http
|
||||
.csrf(AbstractHttpConfigurer::disable)
|
||||
.authorizeHttpRequests(auth -> auth
|
||||
.anyRequest().permitAll()
|
||||
);
|
||||
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
-9
@@ -1,9 +0,0 @@
|
||||
package aplp.backend.lms.infrastructure.persistence.repositories;
|
||||
|
||||
import aplp.backend.lms.domain.entities.Course;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface CourseJpaRepository extends JpaRepository<Course, Long> {
|
||||
}
|
||||
-35
@@ -1,35 +0,0 @@
|
||||
package aplp.backend.lms.infrastructure.persistence.repositories;
|
||||
|
||||
import aplp.backend.lms.domain.entities.Course;
|
||||
import aplp.backend.lms.domain.repositories.CourseRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
||||
public class CourseRepositoryImpl implements CourseRepository {
|
||||
private final CourseJpaRepository repository;
|
||||
|
||||
@Override
|
||||
public Optional<Course> findById(Long id) {
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Course> findAll() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Course save(Course course) {
|
||||
return repository.save(course);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(Long id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,13 @@ spring:
|
||||
name: lms
|
||||
|
||||
datasource:
|
||||
url: jdbc:postgresql://localhost:5432/aplp
|
||||
url: jdbc:postgresql://192.168.2.100:5432/aplp
|
||||
username: postgres
|
||||
password: Pa55w0rd
|
||||
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: validate
|
||||
ddl-auto: update
|
||||
properties:
|
||||
hibernate:
|
||||
default_schema: lms
|
||||
@@ -17,7 +17,7 @@ spring:
|
||||
show-sql: true
|
||||
|
||||
flyway:
|
||||
enabled: true
|
||||
enabled: false
|
||||
default-schema: lms
|
||||
schemas: lms
|
||||
locations: classpath:db/migration
|
||||
|
||||
Reference in New Issue
Block a user