From 261298aae37e2e453f2c89681b4626e8c5ab1ae4 Mon Sep 17 00:00:00 2001 From: Abdellah Lamrabat Date: Sun, 15 Dec 2024 14:00:46 +0100 Subject: [PATCH 1/3] feat: create image entity --- .../udl/eps/softarch/demo/domain/Image.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/cat/udl/eps/softarch/demo/domain/Image.java diff --git a/src/main/java/cat/udl/eps/softarch/demo/domain/Image.java b/src/main/java/cat/udl/eps/softarch/demo/domain/Image.java new file mode 100644 index 0000000..dac8e20 --- /dev/null +++ b/src/main/java/cat/udl/eps/softarch/demo/domain/Image.java @@ -0,0 +1,34 @@ +package cat.udl.eps.softarch.demo.domain; + +import com.fasterxml.jackson.annotation.JsonIdentityReference; +import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = true) +@Entity +@Data +public class Image extends UriEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @NotBlank + private String filename; + + @Column(length = 5 * 1024 * 1024) // 5MB + @Size(max = 5 * 1024 * 1024) // 5MB + private String content; + + @JsonIdentityReference(alwaysAsId = true) + @ManyToOne + private Apartment apartment; + + @Override + public Object getId() { + return id; + } +} From 046b0474b561cefe824588d3ede319ef5f4840b9 Mon Sep 17 00:00:00 2001 From: Abdellah Lamrabat Date: Sun, 15 Dec 2024 14:00:56 +0100 Subject: [PATCH 2/3] feat: create image repository --- .../demo/repository/ImageRepository.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/main/java/cat/udl/eps/softarch/demo/repository/ImageRepository.java diff --git a/src/main/java/cat/udl/eps/softarch/demo/repository/ImageRepository.java b/src/main/java/cat/udl/eps/softarch/demo/repository/ImageRepository.java new file mode 100644 index 0000000..67b2dd7 --- /dev/null +++ b/src/main/java/cat/udl/eps/softarch/demo/repository/ImageRepository.java @@ -0,0 +1,20 @@ +package cat.udl.eps.softarch.demo.repository; + + +import cat.udl.eps.softarch.demo.domain.Apartment; +import cat.udl.eps.softarch.demo.domain.Image; +import org.jetbrains.annotations.NotNull; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.repository.query.Param; + +import java.util.List; +import java.util.Optional; + +public interface ImageRepository extends CrudRepository, PagingAndSortingRepository { + @NotNull + Optional findById(@NotNull @Param("id") Long id); + List findByApartment(@Param("country") Apartment Apartment); + List findByFilename(@Param("filename") String filename); + List findByContent(@Param("content") String content); +} From dde1763406c3be3c95976587d8c98dc2d28c4df3 Mon Sep 17 00:00:00 2001 From: Abdellah Lamrabat Date: Sun, 15 Dec 2024 14:01:14 +0100 Subject: [PATCH 3/3] feat: add image in WebSecurityConfig --- .../cat/udl/eps/softarch/demo/config/WebSecurityConfig.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/cat/udl/eps/softarch/demo/config/WebSecurityConfig.java b/src/main/java/cat/udl/eps/softarch/demo/config/WebSecurityConfig.java index f794956..f7bf677 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/config/WebSecurityConfig.java +++ b/src/main/java/cat/udl/eps/softarch/demo/config/WebSecurityConfig.java @@ -38,6 +38,8 @@ protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exce .requestMatchers(HttpMethod.PATCH, "/advertisementStatuses/*").hasAnyRole("ADMIN") .requestMatchers(HttpMethod.POST, "/users").anonymous() .requestMatchers(HttpMethod.POST, "/users/*").denyAll() + .requestMatchers(HttpMethod.POST, "/images").hasAuthority("ROLE_OWNER") + .requestMatchers(HttpMethod.POST, "/images/").hasAnyRole("OWNER") .requestMatchers(HttpMethod.POST, "/rooms").authenticated() .requestMatchers(HttpMethod.POST, "/rooms/*").hasAnyRole("OWNER") .requestMatchers(HttpMethod.PATCH, "/rooms").authenticated()