WebsiteChannelController.java 3.0 KB
// website/controller/WebsiteChannelController.java
package com.aigeo.website.controller;

import com.aigeo.website.entity.WebsiteChannel;
import com.aigeo.website.service.WebsiteChannelService;
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/website-channels")
public class WebsiteChannelController {
    
    @Autowired
    private WebsiteChannelService websiteChannelService;
    
    @GetMapping("/project/{projectId}")
    public List<WebsiteChannel> getChannelsByProjectId(@PathVariable Integer projectId) {
        return websiteChannelService.getChannelsByProjectId(projectId);
    }
    
    @GetMapping("/project/{projectId}/parent/{parentId}")
    public List<WebsiteChannel> getChannelsByProjectIdAndParentId(
            @PathVariable Integer projectId, @PathVariable Integer parentId) {
        return websiteChannelService.getChannelsByProjectIdAndParentId(projectId, parentId);
    }
    
    @GetMapping("/parent/{parentId}")
    public List<WebsiteChannel> getChannelsByParentId(@PathVariable Integer parentId) {
        return websiteChannelService.getChannelsByParentId(parentId);
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<WebsiteChannel> getChannelById(@PathVariable Integer id) {
        Optional<WebsiteChannel> channel = websiteChannelService.getChannelById(id);
        return channel.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    @PostMapping
    public WebsiteChannel createChannel(@RequestBody WebsiteChannel channel) {
        return websiteChannelService.saveChannel(channel);
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<WebsiteChannel> updateChannel(@PathVariable Integer id, 
                                                     @RequestBody WebsiteChannel channelDetails) {
        Optional<WebsiteChannel> channel = websiteChannelService.getChannelById(id);
        if (channel.isPresent()) {
            WebsiteChannel updatedChannel = channel.get();
            updatedChannel.setName(channelDetails.getName());
            updatedChannel.setPath(channelDetails.getPath());
            updatedChannel.setChannelType(channelDetails.getChannelType());
            updatedChannel.setDisplayOrder(channelDetails.getDisplayOrder());
            updatedChannel.setIsVisibleInNav(channelDetails.getIsVisibleInNav());
            updatedChannel.setParentId(channelDetails.getParentId());
            
            WebsiteChannel savedChannel = websiteChannelService.saveChannel(updatedChannel);
            return ResponseEntity.ok(savedChannel);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteChannel(@PathVariable Integer id) {
        websiteChannelService.deleteChannel(id);
        return ResponseEntity.noContent().build();
    }
}