Skip to content
Snippets Groups Projects
Commit 28ffc91a authored by evilimkova's avatar evilimkova Committed by Martin Gargalovič
Browse files

Certificate Create and FindById

parent 4c1536e3
No related branches found
No related tags found
3 merge requests!31M2,!28M2 user,!27Draft: M2 user
Showing
with 338 additions and 6 deletions
package org.fuseri.model.dto.certificate;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.fuseri.model.dto.common.DomainObjectDto;
import java.time.Instant;
/**
* This class represents a Data Transfer Object (DTO) for Certificate entities.
* It is used to transfer Certificate data between different layers of the application.
* It extends the DomainObjectDto class and includes additional Certificate-specific fields.
*/
@Getter
@Setter
public class CertificateSimpleDto extends DomainObjectDto {
@NotBlank
@NotNull
private String userId;
@NotBlank
@NotNull
private Instant generatedAt;
@NotBlank
@NotNull
private String courseId;
@NotBlank
@NotNull
private String certificateFile;
@NotBlank
@NotNull
private String certificateFileName;
public CertificateSimpleDto(String id, String userId, Instant generatedAt, String courseId, String certificateFile, String certificateFileName) {
this.setId(id);
this.userId = userId;
this.generatedAt = generatedAt;
this.courseId = courseId;
this.certificateFile = certificateFile;
this.certificateFileName = certificateFileName;
}
}
......@@ -61,6 +61,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
......
package org.fuseri.modulecertificate;
import jakarta.persistence.*;
import java.io.Serializable;
import java.time.Instant;
import java.util.Objects;
@Entity
@Table(name = "certificate")
public class Certificate implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_certificate")
private Long id;
@Column(name = "id_user")
private String userId;
@Column(name = "generatedAt")
@Temporal(TemporalType.TIMESTAMP)
private Instant generatedAt;
@Column(name = "id_course")
private String courseId;
@Column(name = "certificate_file")
private String certificateFile;
@Column(name = "certificate_file_name")
private String certificateFileName;
public Certificate() {
}
public Certificate(String userId, Instant generatedAt, String courseId, String certificateFile, String certificateFileName) {
this.userId = userId;
this.generatedAt = generatedAt;
this.courseId = courseId;
this.certificateFile = certificateFile;
this.certificateFileName = certificateFileName;
}
public Long getId() {
return id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Instant getGeneratedAt() {
return generatedAt;
}
public void setGeneratedAt(Instant generatedAt) {
this.generatedAt = generatedAt;
}
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getCertificateFile() {
return certificateFile;
}
public void setCertificateFile(String certificateFile) {
this.certificateFile = certificateFile;
}
public String getCertificateFileName() {
return certificateFileName;
}
public void setCertificateFileName(String certificateFileName) {
this.certificateFileName = certificateFileName;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Certificate certificate)) {
return false;
}
return Objects.equals(getUserId(), certificate.getUserId())
&& Objects.equals(getGeneratedAt(), certificate.getGeneratedAt())
&& Objects.equals(getCourseId(), certificate.getCourseId())
&& Objects.equals(getCertificateFile(), certificate.getCertificateFile())
&& Objects.equals(getCertificateFileName(), certificate.getCertificateFileName());
}
@Override
public int hashCode() {
return Objects.hash(getUserId(), getGeneratedAt(), getCourseId(), getCertificateFile(), getCertificateFileName());
}
}
......@@ -3,6 +3,7 @@ package org.fuseri.modulecertificate.service;
import jakarta.validation.Valid;
import org.fuseri.model.dto.certificate.CertificateCreateDto;
import org.fuseri.model.dto.certificate.CertificateDto;
import org.fuseri.model.dto.certificate.CertificateSimpleDto;
import org.fuseri.model.dto.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
......@@ -18,10 +19,14 @@ import java.util.List;
@RequestMapping("/certificates")
public class CertificateController {
private final CertificateFacade certificateFacade;
@Autowired
public CertificateController() {
public CertificateController(CertificateFacade certificateFacade) {
this.certificateFacade = certificateFacade;
}
/**
* Generates certificate for specified user and course.
*
......@@ -29,8 +34,8 @@ public class CertificateController {
* @return CertificateDto with data of generated certificate
*/
@PostMapping("/generate")
public CertificateDto generate(@Valid @RequestBody CertificateCreateDto certificateCreateDto) {
return new CertificateDto();
public CertificateSimpleDto generate(@Valid @RequestBody CertificateCreateDto certificateCreateDto) {
return certificateFacade.generate(certificateCreateDto);
}
/**
......@@ -40,8 +45,8 @@ public class CertificateController {
* @return CertificateDto with data of previously generated certificate with specified ID
*/
@GetMapping("/find")
public CertificateDto find(@RequestParam String id) {
return new CertificateDto();
public CertificateSimpleDto find(@RequestParam Long id) {
return certificateFacade.findById(id);
}
/**
......
package org.fuseri.modulecertificate.service;
import org.fuseri.model.dto.certificate.CertificateCreateDto;
import org.fuseri.model.dto.certificate.CertificateDto;
import org.fuseri.model.dto.certificate.CertificateSimpleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class CertificateFacade {
private final CertificateService certificateService;
private final CertificateMapper certificateMapper;
@Autowired
public CertificateFacade(CertificateService certificateService, CertificateMapper certificateMapper) {
this.certificateService = certificateService;
this.certificateMapper = certificateMapper;
}
@Cacheable(cacheNames = "certificates", key = "#id")
@Transactional(readOnly = true)
public CertificateSimpleDto findById(Long id) {
return certificateMapper.mapToSimpleDto(certificateService.findById(id));
}
@Transactional()
public CertificateSimpleDto generate(CertificateCreateDto certificateCreateDto) {
return certificateMapper.mapToSimpleDto(certificateService.save(certificateMapper.mapToCertificate(certificateCreateDto)));
}
//@Transactional(readOnly = true)
//public Page<CertificateDto> findAll(Pageable pageable) {
//return certificateMapper.mapToPageDto(certificateService.findAll(pageable));
//}
}
package org.fuseri.modulecertificate.service;
import org.fuseri.model.dto.certificate.CertificateCreateDto;
import org.fuseri.model.dto.certificate.CertificateSimpleDto;
import org.fuseri.modulecertificate.Certificate;
import org.mapstruct.Mapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import java.time.Instant;
import java.util.List;
@Mapper(componentModel = "spring")
public interface CertificateMapper {
default CertificateSimpleDto mapToSimpleDto(Certificate certificate)
{
if ( certificate == null ) {
return null;
}
return new CertificateSimpleDto(
certificate.getId().toString(),
certificate.getUserId(),
certificate.getGeneratedAt(),
certificate.getCourseId(),
certificate.getCertificateFile(),
certificate.getCertificateFileName());
}
default Certificate mapToCertificate(CertificateCreateDto certificateCreateDto)
{
if ( certificateCreateDto == null ) {
return null;
}
return new Certificate(certificateCreateDto.getUser().getId(),
Instant.now(),
certificateCreateDto.getCourse().getId(),
null,
null);
}
//List<CertificateDto> mapToList(List<Certificate> certificates);
//default Page<CertificateDto> mapToPageDto(Page<Certificate> certificates) {
//return new PageImpl<>(mapToList(certificates.getContent()), certificates.getPageable(), certificates.getTotalPages());
//}
}
package org.fuseri.modulecertificate.service;
import org.fuseri.modulecertificate.Certificate;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CertificateRepository extends JpaRepository<Certificate, Long> {
}
package org.fuseri.modulecertificate.service;
import org.fuseri.modulecertificate.Certificate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class CertificateService {
private final CertificateRepository certificateRepository;
@Autowired
public CertificateService(CertificateRepository certificateRepository) {
this.certificateRepository = certificateRepository;
}
@Transactional(readOnly = true)
public Certificate findById(Long id) {
return certificateRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Certificate with id: " + id + " was not found."));
}
@Transactional(readOnly = true)
public Certificate save(Certificate certificate) {
return certificateRepository.save(certificate);
}
@Transactional(readOnly = true)
public Page<Certificate> findAll(Pageable pageable) {
return certificateRepository.findAll(pageable);
}
}
package org.fuseri.modulecertificate.service;
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException() {
}
public ResourceNotFoundException(String message) {
super(message);
}
public ResourceNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public ResourceNotFoundException(Throwable cause) {
super(cause);
}
public ResourceNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
server.port=5001
\ No newline at end of file
server.port=5001
spring.jpa.open-in-view=false
spring.datasource.url=jdbc:h2:mem:social-network;MODE=PostgreSQL
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=SedaQ-app
spring.datasource.password=$argon2id$v=19$m=16,t=2,p=1$YmF0bWFuYmF0bWFu$MdHYB359HdivAb9J6CaILw
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# showing SQL is generally good practice for running project locally to check whether there is not an issue with implementation of JPA methods.
spring.jpa.show-sql=true
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment