package com.royalrangers.controller;

import com.royalrangers.dto.ResponseResult;
import com.royalrangers.dto.structure.*;
import com.royalrangers.exception.EntryAlreadyExistsException;
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 {
            return ResponseBuilder.success(structureService.createCountry(countryDto));
        } catch (EntryAlreadyExistsException e) {
            return ResponseBuilder.fail(e.getMessage());
        } catch (Exception e) {
            return ResponseBuilder.fail("Error creating new country");
        }
    }

    @PostMapping("/region")
    @ApiOperation(value = "Add regions")
    public ResponseResult addRegion(@RequestBody RegionDto regionDto) {
        try {
            return ResponseBuilder.success(structureService.createRegion(regionDto));
        } catch (EntryAlreadyExistsException e) {
            return ResponseBuilder.fail(e.getMessage());
        } catch (Exception e) {
            return ResponseBuilder.fail("Error creating new region");
        }
    }

    @PostMapping("/city")
    @ApiOperation(value = "Add city")
    public ResponseResult addCity(@RequestBody CityDto cityDto) {
        try {
            return ResponseBuilder.success(structureService.createCity(cityDto));
        } catch (EntryAlreadyExistsException e) {
            return ResponseBuilder.fail(e.getMessage());
        } catch (Exception e) {
            return ResponseBuilder.fail("Error creating new city.");
        }
    }

    @PostMapping("/platoon")
    @ApiOperation(value = "Add platoon")
    public ResponseResult addPlatoon(@RequestBody PlatoonDto platoonDto) {
        try {
            return ResponseBuilder.success(structureService.createPlatoon(platoonDto));
        } catch (EntryAlreadyExistsException e) {
            return ResponseBuilder.fail(e.getMessage());
        } catch (Exception e) {
            return ResponseBuilder.fail("Error creating new platoon.");
        }
    }

    @PostMapping("/section")
    @ApiOperation(value = "Add section")
    public ResponseResult addSection(@RequestBody SectionDto sectionDto) {
        try {
            return ResponseBuilder.success(structureService.createSection(sectionDto));
        } catch (EntryAlreadyExistsException e) {
            return ResponseBuilder.fail(e.getMessage());
        } catch (Exception e) {
            return ResponseBuilder.fail("Error creating new section.");
        }
    }
}

@@ -117,9 +117,164 @@
 ure.
-*
+CityDto;%0Aimport com.royalrangers.dto.structure.CountryDto;%0Aimport com.royalrangers.dto.structure.RegionDto;%0Aimport com.royalrangers.dto.structure.SectionDto
 ;%0Aim
@@ -2458,483 +2458,8 @@
  %7D%0A%0A
-    @PostMapping(%22/platoon%22)%0A    @ApiOperation(value = %22Add platoon%22)%0A    public ResponseResult addPlatoon(@RequestBody PlatoonDto platoonDto) %7B%0A        try %7B%0A            return ResponseBuilder.success(structureService.createPlatoon(platoonDto));%0A        %7D catch (EntryAlreadyExistsException e) %7B%0A            return ResponseBuilder.fail(e.getMessage());%0A        %7D catch (Exception e) %7B%0A            return ResponseBuilder.fail(%22Error creating new platoon.%22);%0A        %7D%0A    %7D%0A%0A
     

Revert "Add "addPlatoon" endpoint to admin controller"