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() 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; + } +} 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); +}