LandingPageAssetController.java 2.7 KB
// landingpage/controller/LandingPageAssetController.java
package com.aigeo.landingpage.controller;

import com.aigeo.landingpage.entity.LandingPageAsset;
import com.aigeo.landingpage.entity.AssetType;
import com.aigeo.landingpage.service.LandingPageAssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/landing-page-assets")
public class LandingPageAssetController {
    
    @Autowired
    private LandingPageAssetService landingPageAssetService;
    
    @GetMapping("/project/{projectId}")
    public List<LandingPageAsset> getAssetsByProjectId(@PathVariable Integer projectId) {
        return landingPageAssetService.getAssetsByProjectId(projectId);
    }
    
    @GetMapping("/project/{projectId}/type/{assetType}")
    public List<LandingPageAsset> getAssetsByProjectIdAndAssetType(
            @PathVariable Integer projectId, @PathVariable AssetType assetType) {
        return landingPageAssetService.getAssetsByProjectIdAndAssetType(projectId, assetType);
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<LandingPageAsset> getAssetById(@PathVariable Integer id) {
        Optional<LandingPageAsset> asset = landingPageAssetService.getAssetById(id);
        return asset.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    @PostMapping
    public LandingPageAsset createAsset(@RequestBody LandingPageAsset asset) {
        return landingPageAssetService.saveAsset(asset);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<LandingPageAsset> updateAsset(@PathVariable Integer id, 
                                                     @RequestBody LandingPageAsset assetDetails) {
        Optional<LandingPageAsset> asset = landingPageAssetService.getAssetById(id);
        if (asset.isPresent()) {
            LandingPageAsset updatedAsset = asset.get();
            updatedAsset.setAssetType(assetDetails.getAssetType());
            updatedAsset.setUploadedFileId(assetDetails.getUploadedFileId());
            updatedAsset.setRelatedData(assetDetails.getRelatedData());
            
            LandingPageAsset savedAsset = landingPageAssetService.saveAsset(updatedAsset);
            return ResponseEntity.ok(savedAsset);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAsset(@PathVariable Integer id) {
        landingPageAssetService.deleteAsset(id);
        return ResponseEntity.noContent().build();
    }
}