package com.sqx.modules.announcement.controller;

import com.sqx.common.utils.Result;
import com.sqx.modules.announcement.service.AnnouncementService;
import com.sqx.modules.common.dto.AddAnnouncementDTO;
import com.sqx.modules.common.dto.DeleteAnnouncementDTO;
import com.sqx.modules.common.dto.UpdateAnnouncementDTO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    private final AnnouncementService announcementService;

    public AnnouncementController(AnnouncementService announcementService) {
        this.announcementService = announcementService;
    }

    @GetMapping("/list")
    public Result list(@RequestParam(required = false) String title, @RequestParam(required = false) Integer state, @RequestParam(required = false) Integer id) {
        return Result.success().put("data", announcementService.listInfo(title, state, id));
    }

    @PostMapping
    public Result add(@RequestBody @Validated AddAnnouncementDTO announcementDTO) {
        return Result.success().put("data", announcementService.add(announcementDTO));
    }

    @PutMapping
    public Result update(@RequestBody @Validated UpdateAnnouncementDTO updateAnnouncementDTO) {
        return Result.success().put("data", announcementService.updateInfo(updateAnnouncementDTO));
    }

    @DeleteMapping
    public Result delete(@RequestBody @Validated DeleteAnnouncementDTO deleteAnnouncementDTO) {
        return Result.success().put("data", announcementService.removeById(deleteAnnouncementDTO.getId()));
    }

}
