package com.bofeng.controller;

import com.bofeng.Consts;
import com.bofeng.entity.Place;
import com.bofeng.service.PlaceService;
import com.google.common.base.Strings;
import com.yvan.Model;
import com.yvan.ModelOps;
import com.yvan.mvc.Pd;
import lombok.val;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequiresPermissions(Consts.USER_TYPE_ADMIN)
public class AdminPlaceController {

    @Autowired
    private PlaceService placeService;

    @GetMapping("/admin/place.html")
    public ModelAndView placeView() {
        return new ModelAndView("/admin/place.ftl");
    }

    @GetMapping("/admin/place.json")
    public Model<List<Place>> getPlace() {
        return Model.newSuccess(placeService.selectAll());
    }

    @PostMapping("/admin/place_addnew.json")
    public ModelOps placeAddNew(@RequestBody Place place) {
        val msg = placeService.insert(place);
        if (Strings.isNullOrEmpty(msg)) {
            return ModelOps.newSuccess();
        }
        return ModelOps.newFail(msg);
    }

    @PostMapping("/admin/place_edit")
    public ModelOps placeEdit(@RequestBody Place place) {
        val msg = placeService.updateById(place);
        if (Strings.isNullOrEmpty(msg)) {
            return ModelOps.newSuccess();
        }
        return ModelOps.newFail(msg);
    }

    @PostMapping("/admin/place_delete.json")
    public ModelOps placeDelete(@Pd(name = "placeId", desc = "") Long placeId) {
        return ModelOps.newSuccess(placeService.deleteById(placeId));
    }
}
