Skip to content

Commit

Permalink
[FEAT] (apicli) cli working
Browse files Browse the repository at this point in the history
  • Loading branch information
Binoui committed May 11, 2018
1 parent 581cbc6 commit 767625e
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@

import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Controller;

import com.excilys.formation.cdb.config.ConsoleConfig;
import com.excilys.formation.cdb.dto.CompanyDTO;
import com.excilys.formation.cdb.dto.ComputerDTO;
import com.excilys.formation.cdb.mapper.CompanyDTOMapper;
import com.excilys.formation.cdb.mapper.ComputerDTOMapper;
import com.excilys.formation.cdb.model.Company;
import com.excilys.formation.cdb.model.Company.CompanyBuilder;
import com.excilys.formation.cdb.model.Computer;
import com.excilys.formation.cdb.model.Computer.ComputerBuilder;
import com.excilys.formation.cdb.pagination.CompanyListPage;
import com.excilys.formation.cdb.pagination.ComputerListPage;
import com.excilys.formation.cdb.pagination.Page;
import com.excilys.formation.cdb.services.CompanyService;
import com.excilys.formation.cdb.services.ComputerService;
import com.excilys.formation.cdb.services.ServiceException;
import com.excilys.formation.cdb.validators.IncorrectValidationException;
import com.excilys.formation.cdb.validators.InvalidDatesException;

interface DTOMapper {
Object FromDTO(Object a);
}

@Controller
public class CommandLineInterface {

Expand All @@ -47,15 +49,11 @@ public static void main(String[] arg) {
}

private Client client;
private CompanyService companyService;
private ComputerService computerService;
private Scanner scanner;
private static final String REST_URL = "http://localhost:8080/cdb-webservice/";

public CommandLineInterface(CompanyService companyService, ComputerService computerService) {
public CommandLineInterface() {
scanner = new Scanner(System.in);
this.companyService = companyService;
this.computerService = computerService;
this.client = ClientBuilder.newClient();
}

Expand All @@ -66,50 +64,58 @@ private void closeScanner() {
private void createComputer() {
Computer c = new Computer();
readComputer(c);

try {
c.setId(computerService.createComputer(c));
System.out.println("Created new computer with ID " + c.getId());
} catch (ServiceException | IncorrectValidationException e) {
Logger.error("couldnt create computer {}", e.getMessage());
}
client.target(REST_URL).path("computer/").request().post(Entity.entity(ComputerDTOMapper.createComputerDTO(c), MediaType.APPLICATION_JSON));
System.out.println("Created new computer");
}

private void deleteCompany() {
Long id = readNotNullId();
companyService.deleteCompany(id);
client.target(REST_URL).path("company/" + id).request().delete();
System.out.println("Company deleted !");
}

private void deleteComputer() {
Long id = readNotNullId();
Computer c = new ComputerBuilder().withId(id).build();
try {
computerService.deleteComputer(c);
} catch (ServiceException e) {
Logger.error("couldnt create computer {}", e.getMessage());
}
}

private void getCompanyList() {
System.out.println("******** Companies List ********");

try {
readPages(new CompanyListPage(companyService));
} catch (ServiceException e) {
Logger.error("couldnt create computer {}", e.getMessage());
}
client.target(REST_URL).path("computer/" + id).request().delete();
System.out.println("Computer deleted !");
}

private void getComputerList() {
System.out.println("******** Computer List ********");

int pageSize = 10;
int pageCount = client.target(REST_URL).path("computers/size/" + pageSize + "/count")
.request(MediaType.APPLICATION_JSON).get(Integer.class);
GenericType<List<ComputerDTO>> genericType = new GenericType<List<ComputerDTO>>() {
};
WebTarget computersPath = client.target(REST_URL).path("computers");
try {
readPages(new ComputerListPage(computerService));
} catch (ServiceException e) {
Logger.error("couldnt create computer {}", e.getMessage());
readPages(computersPath, genericType, (c -> {
try {
return ComputerDTOMapper.createComputerFromDto((ComputerDTO) c);
} catch (InvalidDatesException e) {

}
return null;
}), pageCount, pageSize);
} catch (Exception e) {

}
}

private void getCompanyList() {
System.out.println("******** Companies List ********");
int pageSize = 10;
int pageCount = client.target(REST_URL).path("companies/size/" + pageSize + "/count")
.request(MediaType.APPLICATION_JSON).get(Integer.class);
Logger.debug("pageCount : " + pageCount);
GenericType<List<CompanyDTO>> genericType = new GenericType<List<CompanyDTO>>() {
};
WebTarget companiesPath = client.target(REST_URL).path("companies");
readPages(companiesPath, genericType, (c -> CompanyDTOMapper.createCompanyFromDto((CompanyDTO) c)), pageCount,
pageSize);
}

private void getDetailsComputer() {
Long id = readNotNullId();
ComputerDTO c = null;
Expand Down Expand Up @@ -254,36 +260,38 @@ private Long readNotNullId() {
return id;
}

private <T extends Page<?>> void readPages(T page) {
private <T> void readPages(WebTarget restPath, GenericType<List<T>> genericType, DTOMapper dtoMapper, int pageCount,
int pageSize) {
String choice = "f";

int page = 0;
while (!choice.equals("q")) {

try {
switch (choice) {
case "n":
page.next().forEach(System.out::println);
break;
case "p":
page.previous().forEach(System.out::println);
break;
case "f":
page.goToFirst().forEach(System.out::println);
break;
case "l":
page.goToLast().forEach(System.out::println);
break;
case "q":
System.out.println("Closing.");
break;
default:
System.out.println("Wrong choice.");
}
} catch (ServiceException e) {
Logger.error("Error while accessing pages {}", e);
System.out.println("Error while accessing pages");
switch (choice) {
case "n":
if (page < pageCount - 1)
page++;
break;
case "p":
if (page >= 0)
page--;
break;
case "f":
page = 0;
break;
case "l":
page = pageCount - 1;
break;
case "q":
System.out.println("Closing.");
return;
default:
System.out.println("Wrong choice.");
}

Response responseEntity = restPath.path("/page/" + page).path("/size/" + pageSize)
.request(MediaType.APPLICATION_JSON).get(Response.class);
responseEntity.readEntity(genericType).stream().map(c -> dtoMapper.FromDTO(c)).forEach(System.out::println);

System.out.println("Reading Pages. Possible choices : [n]ext, [p]revious, [f]irst, [l]ast, [q]uit");
choice = scanner.nextLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public interface CompanyRestController {

List<CompanyDTO> getCompanies();

ResponseEntity<Integer> getCompanyPageCount(int size);

ResponseEntity<List<CompanyDTO>> getCompanyPage(int page, int size);

ResponseEntity<String> deleteCompany(long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -48,4 +49,26 @@ public List<CompanyDTO> getCompanies() {
return companyService.getListCompanies().stream().map((Company c) -> CompanyDTOMapper.createCompanyDTO(c))
.collect(Collectors.toList());
}

@Override
@GetMapping(value = "/companies/size/{size}/count")
public ResponseEntity<Integer> getCompanyPageCount(@PathVariable int size) {
return new ResponseEntity<>(companyService.getListCompaniesPageCount(size), HttpStatus.OK);
}

@Override
@GetMapping(value = "/companies/page/{page}/size/{size}")
public ResponseEntity<List<CompanyDTO>> getCompanyPage(@PathVariable int page, @PathVariable int size) {
return new ResponseEntity<>(companyService.getListCompanies(page, size)
.stream().map(c -> CompanyDTOMapper.createCompanyDTO(c)).collect(Collectors.toList()), HttpStatus.OK);
}

@Override
@DeleteMapping(value = "/computer/{id}")
public ResponseEntity<String> deleteCompany(@PathVariable long id) {
ResponseEntity<String> response;
companyService.deleteCompany(id);
response = new ResponseEntity<>(HttpStatus.OK);
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.excilys.formation.cdb.dto.ComputerDTO;
import com.excilys.formation.cdb.mapper.ComputerDTOMapper;
import com.excilys.formation.cdb.model.Computer;
import com.excilys.formation.cdb.services.CompanyService;
import com.excilys.formation.cdb.services.ComputerService;
import com.excilys.formation.cdb.services.ServiceException;
import com.excilys.formation.cdb.validators.IncorrectValidationException;
Expand Down Expand Up @@ -63,28 +64,28 @@ public List<ComputerDTO> getComputers() {
return computerService.getListComputers().stream().map((Computer c) -> ComputerDTOMapper.createComputerDTO(c))
.collect(Collectors.toList());
}

@Override
@GetMapping(value = "/computer/size/{size}/count")
@GetMapping(value = "/computers/size/{size}/count")
public ResponseEntity<Integer> getComputerPageCount(@PathVariable int size) {
return new ResponseEntity<>(computerService.getListComputersPageCount(size), HttpStatus.OK);
}

@Override
@GetMapping(value = "/computer/page/{page}/size/{size}")
@GetMapping(value = "/computers/page/{page}/size/{size}")
public ResponseEntity<List<ComputerDTO>> getComputerPage(@PathVariable int page, @PathVariable int size) {
return new ResponseEntity<>(computerService.getListComputers(page, size, SortableComputerColumn.ID, true)
.stream().map(c -> ComputerDTOMapper.createComputerDTO(c)).collect(Collectors.toList()), HttpStatus.OK);
}

@Override
@GetMapping(value = "/computer/size/{size}/search/{search}")
@GetMapping(value = "/computers/size/{size}/search/{search}")
public ResponseEntity<Integer> getComputerPageSearchCount(@PathVariable int page, @PathVariable String search) {
return new ResponseEntity<>(computerService.getListComputersPageCount(page, search), HttpStatus.OK);
}

@Override
@GetMapping(value = "/computer/page/{page}/size/{size}/search/{search}")
@GetMapping(value = "/computers/page/{page}/size/{size}/search/{search}")
public ResponseEntity<List<ComputerDTO>> getComputerPageSearch(@PathVariable int page, @PathVariable int size,
@PathVariable String search) {
return new ResponseEntity<>(
Expand All @@ -94,15 +95,15 @@ public ResponseEntity<List<ComputerDTO>> getComputerPageSearch(@PathVariable int
}

@Override
@GetMapping(value = "/computer/page/{page}/size/{size}/column/{column}/ascending/{ascending}")
@GetMapping(value = "/computers/page/{page}/size/{size}/column/{column}/ascending/{ascending}")
public ResponseEntity<List<ComputerDTO>> getComputerPageSorted(@PathVariable int page, @PathVariable int size,
@PathVariable SortableComputerColumn column, @PathVariable boolean ascending) {
return new ResponseEntity<>(computerService.getListComputers(page, size, column, ascending).stream()
.map(c -> ComputerDTOMapper.createComputerDTO(c)).collect(Collectors.toList()), HttpStatus.OK);
}

@Override
@GetMapping(value = "/computer/page/{page}/size/{size}/search/{search}/column/{column}/ascending/{ascending}")
@GetMapping(value = "/computers/page/{page}/size/{size}/search/{search}/column/{column}/ascending/{ascending}")
public ResponseEntity<List<ComputerDTO>> getComputerPageSortedSearch(@PathVariable int page, @PathVariable int size,
@PathVariable String search, @PathVariable SortableComputerColumn column, @PathVariable boolean ascending) {
return new ResponseEntity<>(computerService.getListComputers(page, size, column, ascending, search).stream()
Expand All @@ -116,7 +117,7 @@ public ResponseEntity<String> createComputer(@RequestBody ComputerDTO computerDT
ResponseEntity<String> response;
try {
computerService.createComputer(ComputerDTOMapper.createComputerFromDto(computerDTO));
response = new ResponseEntity<>(HttpStatus.OK);
response = new ResponseEntity<>(HttpStatus.CREATED);
} catch (ServiceException | IncorrectValidationException e) {
LOGGER.debug("couldn't create computer {}", e);
response = new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
Expand All @@ -142,12 +143,12 @@ public ResponseEntity<String> editComputer(@RequestBody ComputerDTO computerDto)
}

@Override
@DeleteMapping(value = "/computer")
@DeleteMapping(value = "/computer/{id}")
public ResponseEntity<String> deleteComputer(@PathVariable long id) {
ResponseEntity<String> response;

try {
computerService.deleteComputer(id);
CompanyService.deleteComputer(id);
response = new ResponseEntity<>(HttpStatus.OK);
} catch (ServiceException e) {
response = new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
Expand Down

0 comments on commit 767625e

Please sign in to comment.