/*
 * Copyright (C) 2018 the original author or authors.
 *
 * This file is part of jBB Application Project.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 */

package org.jbb.security.rest.lockout;

import static org.jbb.lib.restful.RestAuthorize.IS_AN_ADMINISTRATOR;
import static org.jbb.lib.restful.RestConstants.API_V1;
import static org.jbb.lib.restful.domain.ErrorInfo.ACTIVE_MEMBER_LOCK_NOT_FOUND;
import static org.jbb.lib.restful.domain.ErrorInfo.FORBIDDEN;
import static org.jbb.lib.restful.domain.ErrorInfo.MEMBER_NOT_FOUND;
import static org.jbb.lib.restful.domain.ErrorInfo.UNAUTHORIZED;
import static org.jbb.security.rest.SecurityRestConstants.ACTIVE_LOCK;
import static org.jbb.security.rest.SecurityRestConstants.MEMBERS;
import static org.jbb.security.rest.SecurityRestConstants.MEMBER_ID;
import static org.jbb.security.rest.SecurityRestConstants.MEMBER_ID_VAR;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.jbb.lib.restful.domain.ErrorInfoCodes;
import org.jbb.lib.restful.error.ErrorResponse;
import org.jbb.members.api.base.Member;
import org.jbb.members.api.base.MemberNotFoundException;
import org.jbb.members.api.base.MemberService;
import org.jbb.security.api.lockout.MemberLock;
import org.jbb.security.api.lockout.MemberLockoutService;
import org.jbb.security.rest.lockout.exception.MemberLockNotFound;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@PreAuthorize(IS_AN_ADMINISTRATOR)
@Api(tags = API_V1 + MEMBERS + MEMBER_ID + ACTIVE_LOCK)
@RequestMapping(value = API_V1 + MEMBERS + MEMBER_ID
    + ACTIVE_LOCK, produces = MediaType.APPLICATION_JSON_VALUE)
public class ActiveMemberLockResource {

    private final MemberService memberService;
    private final MemberLockoutService memberLockoutService;

    private final MemberLockTranslator translator;

    @GetMapping
    @ErrorInfoCodes({MEMBER_NOT_FOUND, ACTIVE_MEMBER_LOCK_NOT_FOUND, UNAUTHORIZED, FORBIDDEN})
    @ApiOperation("Gets active lock for given member")
    public MemberLockDto activeLockGet(@PathVariable(MEMBER_ID_VAR) Long memberId)
        throws MemberNotFoundException {
        MemberLock lock = getMemberLock(memberId);
        return translator.toDto(lock);
    }

    @DeleteMapping
    @ErrorInfoCodes({MEMBER_NOT_FOUND, ACTIVE_MEMBER_LOCK_NOT_FOUND, UNAUTHORIZED, FORBIDDEN})
    @ApiOperation("Deactivates active lock for given member")
    public void activeLockDelete(@PathVariable(MEMBER_ID_VAR) Long memberId)
        throws MemberNotFoundException {
        getMemberLock(memberId);
        memberLockoutService.releaseMemberLock(memberId);
    }

    private MemberLock getMemberLock(Long memberId) throws MemberNotFoundException {
        Member member = memberService.getMemberWithIdChecked(memberId);
        return memberLockoutService.getMemberActiveLock(member.getId())
            .orElseThrow(MemberLockNotFound::new);
    }

    @ExceptionHandler(MemberLockNotFound.class)
    ResponseEntity<ErrorResponse> handle(MemberLockNotFound ex) {
        return ErrorResponse.getErrorResponseEntity(ACTIVE_MEMBER_LOCK_NOT_FOUND);
    }
}

@@ -319,809 +319,8 @@
 t;%0A%0A
-import static org.jbb.lib.restful.RestAuthorize.IS_AN_ADMINISTRATOR;%0Aimport static org.jbb.lib.restful.RestConstants.API_V1;%0Aimport static org.jbb.lib.restful.domain.ErrorInfo.ACTIVE_MEMBER_LOCK_NOT_FOUND;%0Aimport static org.jbb.lib.restful.domain.ErrorInfo.FORBIDDEN;%0Aimport static org.jbb.lib.restful.domain.ErrorInfo.MEMBER_NOT_FOUND;%0Aimport static org.jbb.lib.restful.domain.ErrorInfo.UNAUTHORIZED;%0Aimport static org.jbb.security.rest.SecurityRestConstants.ACTIVE_LOCK;%0Aimport static org.jbb.security.rest.SecurityRestConstants.MEMBERS;%0Aimport static org.jbb.security.rest.SecurityRestConstants.MEMBER_ID;%0Aimport static org.jbb.security.rest.SecurityRestConstants.MEMBER_ID_VAR;%0A%0Aimport io.swagger.annotations.Api;%0Aimport io.swagger.annotations.ApiOperation;%0Aimport lombok.RequiredArgsConstructor;%0A
 impo
@@ -365,16 +365,16 @@
 oCodes;%0A
+
 import o
@@ -730,16 +730,60 @@
 tFound;%0A
+import org.springframework.http.HttpStatus;%0A
 import o
@@ -1232,32 +1232,95 @@
 RequestMapping;%0A
+import org.springframework.web.bind.annotation.ResponseStatus;%0A
 import org.sprin
@@ -1367,16 +1367,818 @@
 oller;%0A%0A
+import io.swagger.annotations.Api;%0Aimport io.swagger.annotations.ApiOperation;%0Aimport lombok.RequiredArgsConstructor;%0A%0Aimport static org.jbb.lib.restful.RestAuthorize.IS_AN_ADMINISTRATOR;%0Aimport static org.jbb.lib.restful.RestConstants.API_V1;%0Aimport static org.jbb.lib.restful.domain.ErrorInfo.ACTIVE_MEMBER_LOCK_NOT_FOUND;%0Aimport static org.jbb.lib.restful.domain.ErrorInfo.FORBIDDEN;%0Aimport static org.jbb.lib.restful.domain.ErrorInfo.MEMBER_NOT_FOUND;%0Aimport static org.jbb.lib.restful.domain.ErrorInfo.UNAUTHORIZED;%0Aimport static org.jbb.security.rest.SecurityRestConstants.ACTIVE_LOCK;%0Aimport static org.jbb.security.rest.SecurityRestConstants.MEMBERS;%0Aimport static org.jbb.security.rest.SecurityRestConstants.MEMBER_ID;%0Aimport static org.jbb.security.rest.SecurityRestConstants.MEMBER_ID_VAR;%0A%0A
 @RestCon
@@ -3003,24 +3003,24 @@
 ck);%0A    %7D%0A%0A
-
     @DeleteM
@@ -3018,32 +3018,75 @@
  @DeleteMapping%0A
+    @ResponseStatus(HttpStatus.NO_CONTENT)%0A
     @ErrorInfoCo

Add missing response http status no content for delete endpoint