Skip to content

Commit

Permalink
Merge pull request #73 from wethmiranasinghe/main
Browse files Browse the repository at this point in the history
Albums adding button added
  • Loading branch information
wethmiranasinghe authored Sep 6, 2024
2 parents 87440f7 + 814f601 commit 57156ba
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 87 deletions.
11 changes: 7 additions & 4 deletions back-end/src/main/java/com/example/demo/gallery/Gallery.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,25 @@ public class Gallery {
generator = "gallery_sequence"
)

private Long albumId;
private Long albumID;
private String albumName;
private String albumCreatedBy;
private String albumCoverURL;

@Lob
@Column(name = "albumCoverImage", columnDefinition = "LONGBLOB")
private byte[] albumCoverImage;
private String albumURL;

/**
* Constructor to initialize a new Gallery object.
*/
public Gallery(String albumName,
String albumCreatedBy,
String albumCoverURL,
byte[] albumCoverImage,
String albumURL) {
this.albumName = albumName;
this.albumCreatedBy = albumCreatedBy;
this.albumCoverURL = albumCoverURL;
this.albumCoverImage = albumCoverImage;
this.albumURL = albumURL;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.example.demo.gallery;

import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Optional;


/**
* Controller class for managing news and events API endpoints.
* Controller class for managing gallery API endpoints.
*/
@RestController
@RequestMapping("/api/v1/gallery")
Expand All @@ -20,30 +22,39 @@ public class GalleryController {
* Endpoint to fetch all gallery items.
*/
@GetMapping
public List<Gallery> getAllGallery() {
public List<GalleryResponse> getAllGallery() {
return galleryService.getAllGallery();
}

/**
* Endpoint to fetch a specific gallery item by its ID.
*/
@GetMapping("/{albumId}")
public Optional<Gallery> getGalleryById(@PathVariable Long albumId) {
return galleryService.getGalleryById(albumId);
public GalleryResponse getGalleryById(@PathVariable Long albumId) {
return galleryService.getGalleryById(albumId)
.map(GalleryResponse::new)
.orElseThrow(() -> new RuntimeException("Gallery item not found"));
}

/**
* Endpoint to add a new album.
* Endpoint to add a new gallery album with an optional image upload.
*/
@PostMapping
public Gallery addGallery(@RequestBody GalleryRequest galleryRequest) {
return galleryService.addGallery(galleryRequest);
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Gallery addGallery(
@RequestParam("albumName") String albumName,
@RequestParam("albumCreatedBy") String albumCreatedBy,
@RequestParam("albumURL") String albumURL,
@RequestParam("albumCoverImage") MultipartFile albumCoverImage) {

// Call the service to add the gallery with the optional uploaded image
return galleryService.addGallery(albumName, albumCreatedBy, albumURL, albumCoverImage);
}

/**
* Endpoint to delete album by its ID.
* Endpoint to delete a gallery album by its ID.
*/
@DeleteMapping("/{albumId}")
public void deleteGallery(@PathVariable Long albumId) {
galleryService.deleteGallery(albumId);
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.demo.gallery;

import aj.org.objectweb.asm.commons.Remapper;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
Expand All @@ -12,11 +13,18 @@
@Repository
public interface GalleryRepository extends JpaRepository<Gallery, Long> {

/**
* Static method to find a NewsAndEvents entity by its ID.
*/
static Remapper findByAlbumID(Long albumID) {
return null;
}

/**
* Deletes a NewsAndEvents entity by its albumName.
*/
void deleteByAlbumName(String albumName);

@Query("SELECT n FROM Gallery n ORDER BY n.albumId DESC")
@Query("SELECT n FROM Gallery n ORDER BY n.albumID DESC")
List<Gallery> findAllOrderByAlbumIDDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
public class GalleryRequest {
private final String albumName;
private final String albumCreatedBy;
private final String albumCoverURL;
private final byte[] albumCoverImage;
private final String albumURL;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.demo.gallery;

import lombok.Getter;
import lombok.Setter;

import java.util.Base64;

@Getter
@Setter
public class GalleryResponse {

private Long albumID;
private String albumName;
private String albumCreatedBy;
private String albumCoverImage; // Base64 encoded image or URL if applicable
private String albumURL;

public GalleryResponse(Gallery gallery) {
this.albumID = gallery.getAlbumID();
this.albumName = gallery.getAlbumName();
this.albumCreatedBy = gallery.getAlbumCreatedBy();
// Convert image byte array to Base64 encoded string
this.albumCoverImage = Base64.getEncoder().encodeToString(gallery.getAlbumCoverImage());
this.albumURL = gallery.getAlbumURL();
}
}
70 changes: 59 additions & 11 deletions back-end/src/main/java/com/example/demo/gallery/GalleryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,86 @@
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class GalleryService {

private final GalleryRepository galleryRepository;

public List<Gallery> getAllGallery() {
return galleryRepository.findAllOrderByAlbumIDDesc();
/**
* Fetch all gallery items and map them to GalleryResponse objects.
*/
public List<GalleryResponse> getAllGallery() {
return galleryRepository.findAllOrderByAlbumIDDesc()
.stream()
.map(GalleryResponse::new) // Map each entity to its response DTO
.collect(Collectors.toList());
}

/**
* Fetch a specific gallery item by its ID.
*/
public Optional<Gallery> getGalleryById(Long albumId) {
return galleryRepository.findById(albumId);
}

/**
* Add a new gallery item with an optional image upload.
* Handles both text data and the image file.
*
* @param albumName The name of the album.
* @param albumCreatedBy The creator of the album.
* @param albumURL The URL of the album.
* @param albumCoverImage An optional image file to be uploaded.
* @return The saved Gallery entity.
*/
@Transactional
public Gallery addGallery(GalleryRequest galleryRequest) {
public Gallery addGallery(String albumName, String albumCreatedBy, String albumURL,
MultipartFile albumCoverImage) {

byte[] imageBytes = null;
if (albumCoverImage != null && !albumCoverImage.isEmpty()) {
try {
imageBytes = albumCoverImage.getBytes(); // Get the image bytes from MultipartFile
} catch (IOException e) {
throw new RuntimeException("Failed to process image file", e); // Better error handling
}
}

Gallery gallery = new Gallery(
galleryRequest.getAlbumName(),
galleryRequest.getAlbumCreatedBy(),
galleryRequest.getAlbumCoverURL(),
galleryRequest.getAlbumURL()
albumName,
albumCreatedBy,
imageBytes, // Store the image bytes
albumURL
);
System.out.println("Gallery Added");
return galleryRepository.save(gallery);

System.out.println("Gallery Added: " + albumName); // Optional: log the added gallery

return galleryRepository.save(gallery); // Save the entity to the database
}

/**
* Delete a gallery item by its ID.
*
* @param albumID The ID of the gallery item to be deleted.
*/
@Transactional
public void deleteGallery(Long albumId) {
galleryRepository.deleteById(albumId);
public void deleteGallery(Long albumID) {
if (!galleryRepository.existsById(albumID)) {
throw new IllegalArgumentException("Gallery with ID " + albumID + " does not exist."); // Handle non-existing entries
}
galleryRepository.deleteById(albumID); // Perform the delete operation
}

// Uncomment and modify this method if you need to implement update functionality
// public Gallery updateGallery(Long albumId, GalleryRequest updatedRequest) {
// // Fetch existing gallery by ID, update fields, and save.
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.time.LocalDate;

/**
* Represents a news or event entity stored in the database.
Expand All @@ -29,20 +28,16 @@ public class NewsAndEvents {
generator = "news_and_events_sequence"
)
private Long newsID;

private String newsTitle;

@Column(columnDefinition = "TEXT")
private String newsDescription;

private String newsUrl;

@Lob
@Column(name = "newsCoverImage", columnDefinition = "BLOB")
@Column(name = "newsCoverImage", columnDefinition = "LONGBLOB")
private byte[] newsCoverImage;

private String newsDate;

private String newsAuthor;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Optional;


/**
* Controller class for managing news and events API endpoints.
Expand Down
13 changes: 10 additions & 3 deletions back-end/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#configurtion
server:
error:
include-message: always
Expand All @@ -7,7 +6,7 @@ server:
spring:
datasource:
url: jdbc:mysql://localhost:3306/cycle
password:
password:
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
Expand All @@ -24,7 +23,6 @@ spring:
port: 1025
username: hello
password: hello

properties:
mail:
smtp:
Expand All @@ -36,3 +34,12 @@ spring:
connection timeout: 5000
timeout: 3000
write timeout: 5000

servlet:
multipart:
max-file-size: 100MB # Increase size for large files
max-request-size: 100MB # Increase size for large files

tomcat:
max-http-header-size: 16384 # Adjust if needed for large headers
max-swallow-size: 104857600 # 100MB, adjust according to file size needs
Loading

0 comments on commit 57156ba

Please sign in to comment.