package com.royalrangers.controller;

import com.royalrangers.dto.ResponseResult;
import com.royalrangers.dto.structure.RegionDto;
import com.royalrangers.dto.structure.CountryDto;
import com.royalrangers.dto.structure.CityDto;
import com.royalrangers.dto.structure.SectionDto;
import com.royalrangers.service.StructureService;
import com.royalrangers.utils.ResponseBuilder;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/admin")
@PreAuthorize("hasRole('ADMIN')")
public class AdminController {

    @Autowired
    private StructureService structureService;

    @PostMapping("/country")
    @ApiOperation(value = "Add country")
    public ResponseResult addCountry(@RequestBody CountryDto countryDto) {
        try {
            if (!structureService.createCountry(countryDto))
                return ResponseBuilder.fail("Country with this name already exist.");
        } catch (Exception e) {
            return ResponseBuilder.fail("Error creating new country");
        }
        return ResponseBuilder.success("Country '%s' is successfully created.", countryDto.getName());
    }

    @PostMapping("/region")
    @ApiOperation(value = "Add regions")
    public ResponseResult addRegion(@RequestBody RegionDto regionDto) {
        try {
            if (!structureService.createRegion(regionDto))
                return ResponseBuilder.fail("Region with this name already exist.");
        } catch (Exception e) {
            return ResponseBuilder.fail("Error creating new regions");
        }
        return ResponseBuilder.success("Region '%s' is successfully created.", regionDto.getName());
    }

    @PostMapping("/city")
    @ApiOperation(value = "Add city")
    public ResponseResult addCity(@RequestBody CityDto cityDto) {
        try {
            if (!structureService.createCity(cityDto))
                return ResponseBuilder.fail("City with this name already exist.");
        } catch (Exception e) {
            return ResponseBuilder.fail("Error creating new city.");
        }
        return ResponseBuilder.success("City '%s' is successfully created.", cityDto.getName());
    }

    @PostMapping("/section")
    @ApiOperation(value = "Add section")
    public ResponseResult addSection(@RequestBody SectionDto sectionDto) {
        try {
            if (!structureService.createSection(sectionDto))
                return ResponseBuilder.fail("Section with this name already exist.");
        } catch (Exception e) {
            return ResponseBuilder.fail("Error creating section");
        }
        return ResponseBuilder.success("Section '%s' is successfully created.", sectionDto.getName());
    }
}

@@ -1466,20 +1466,44 @@
 Country 
-'%25s'
+%22 + countryDto.getName() + %22
  is succ
@@ -1524,30 +1524,8 @@
 ed.%22
-, countryDto.getName()
 );%0A 
@@ -1985,28 +1985,51 @@
 ess(%22Region 
-'%25s'
+%22 + regionDto.getName() + %22
  is successf
@@ -2046,29 +2046,8 @@
 ed.%22
-, regionDto.getName()
 );%0A 
@@ -2490,20 +2490,41 @@
 s(%22City 
-'%25s'
+%22 + cityDto.getName() + %22
  is succ
@@ -2545,27 +2545,8 @@
 ed.%22
-, cityDto.getName()
 );%0A 
@@ -3018,12 +3018,36 @@
 ion 
-'%25s'
+%22 + sectionDto.getName() + %22
  is 
@@ -3072,30 +3072,8 @@
 ed.%22
-, sectionDto.getName()
 );%0A 

Refactor AdminController