package com.cyksj.web.controller.manage.advertisement;

import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.AdvertisementPopularize;
import com.cyksj.model.entity.AdvertisementProject;
import com.cyksj.service.advertisement.AdvertisementPopularizeService;
import com.cyksj.service.advertisement.AdvertisementProjectService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chan
 * @date 2023/5/16 11:06 AM
 */
@RestController
@RequestMapping("manage/advertisement/project")
@Slf4j
@RequiredArgsConstructor
public class CmsAdvertisementProjectController{


    private final HttpServletRequest request;

    private final BeanSearcher beanSearcher;

    private final AdvertisementProjectService advertisementProjectService;

    @GetMapping
    public Result<SearchResult<AdvertisementProject>> get() {
        SearchResult<AdvertisementProject> searchResult = beanSearcher.search(AdvertisementProject.class,
                MapUtils.flatBuilder(request.getParameterMap())
                        .build());
        return GatewayResponse.SUCCESS.newBuilder().toResult(searchResult);
    }

    @PostMapping
    public Result<AdvertisementProject> post(@RequestBody AdvertisementProject project){
        advertisementProjectService.save(project);
        return GatewayResponse.SUCCESS.newBuilder().toResult(advertisementProjectService.getById(project.getId()));
    }

    @PutMapping
    public Result<String> put(@RequestBody AdvertisementProject project){
        advertisementProjectService.updateById(project);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

    @DeleteMapping("/{id}")
    public Result<String> del(@PathVariable Long id){
        advertisementProjectService.removeById(id);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

}
