jewpms/src/main/java/com/vxnet/pms/web/rest/CompanyResource.java

155 lines
6.2 KiB
Java

package com.vxnet.pms.web.rest;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import com.vxnet.pms.domain.Company;
import com.vxnet.pms.service.CompanyService;
import jakarta.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* REST controller for managing Company.
*/
@RestController
@RequestMapping("/api")
public class CompanyResource {
private final Logger log = LoggerFactory.getLogger(CompanyResource.class);
private final CompanyService companyService;
public CompanyResource(CompanyService companyService) {
this.companyService = companyService;
}
/**
* {@code POST /companies} : Create a new company.
*
* @param company the company to create.
* @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new company.
*/
@PostMapping("/companies")
public Mono<ResponseEntity<Company>> createCompany(@Valid @RequestBody Company company) {
log.debug("REST request to save Company : {}", company);
log.info("Processing create request for Company with name: {}", company.getName());
return companyService
.createCompany(company)
.map(result -> {
try {
log.info("Company created successfully with id: {}", result.getId());
return ResponseEntity.created(new URI("/api/companies/" + result.getId())).body(result);
} catch (URISyntaxException e) {
log.error("Error creating URI for new Company: {}", e.getMessage());
throw new RuntimeException(e);
}
})
.doOnError(error -> log.error("Error in REST layer while creating Company: {}", error.getMessage()));
}
/**
* {@code PUT /companies/:id} : Updates an existing company.
*
* @param id the id of the company to save.
* @param company the company to update.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated company.
*/
@PutMapping("/companies/{id}")
public Mono<ResponseEntity<Company>> updateCompany(@PathVariable(value = "id") final Long id, @Valid @RequestBody Company company) {
log.debug("REST request to update Company : {}, {}", id, company);
log.info("Processing update request for Company with id: {} and name: {}", id, company.getName());
return companyService
.updateCompany(id, company)
.map(result -> {
log.info("Company updated successfully with id: {}", result.getId());
return ResponseEntity.ok().body(result);
})
.doOnError(error -> log.error("Error in REST layer while updating Company: {}", error.getMessage()));
}
/**
* {@code GET /companies} : get all the companies.
*
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of companies in body.
*/
@GetMapping("/companies")
public Mono<ResponseEntity<Flux<Company>>> getAllCompanies(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size
) {
log.debug("REST request to get Companies with pagination - Page: {}, Size: {}", page, size);
Pageable pageable = PageRequest.of(page, size);
return Mono.just(ResponseEntity.ok().body(companyService.findAll(pageable)));
}
public Flux<Company> getAllCompanies() {
log.debug("REST request to get all Companies");
return companyService.getAllCompanies();
}
/**
* {@code GET /companies/:id} : get the "id" company.
*
* @param id the id of the company to retrieve.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the company, or with status {@code 404 (Not Found)}.
*/
@GetMapping("/companies/{id}")
public Mono<ResponseEntity<Company>> getCompany(@PathVariable Long id) {
log.debug("REST request to get Company : {}", id);
return companyService
.getCompany(id)
.map(company -> ResponseEntity.ok().body(company))
.switchIfEmpty(Mono.error(new ResponseStatusException(NOT_FOUND)));
}
/**
* {@code DELETE /companies/:id} : delete the "id" company.
*
* @param id the id of the company to delete.
* @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
*/
@DeleteMapping("/companies/{id}")
public Mono<ResponseEntity<Void>> deleteCompany(@PathVariable Long id) {
log.debug("REST request to delete Company : {}", id);
return companyService.deleteCompany(id).then(Mono.just(ResponseEntity.noContent().build()));
}
/**
* {@code GET /companies/search} : search companies by keyword.
*
* @param keyword the keyword to search for.
* @param page the page number.
* @param size the page size.
* @return the {@link Flux} with status {@code 200 (OK)} and the list of companies in body.
*/
@GetMapping("/companies/search")
public Flux<Company> searchCompanies(
@RequestParam(required = false) String keyword,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size
) {
log.debug("REST request to search Companies with keyword: {}", keyword);
return companyService.searchCompanies(keyword, PageRequest.of(page, size));
}
/**
* {@code GET /companies/active} : get all active companies (with valid licenses).
*
* @return the {@link Flux} with status {@code 200 (OK)} and the list of active companies in body.
*/
@GetMapping("/companies/active")
public Flux<Company> getActiveCompanies() {
log.debug("REST request to get all active Companies");
return companyService.getActiveCompanies();
}
}