Skip to content

Commit 677914d

Browse files
authored
initial commit
1 parent d65f8d0 commit 677914d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2858
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.mazmy;
2+
3+
import com.mazmy.util.LoggingInterceptor;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
8+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
9+
import springfox.documentation.builders.PathSelectors;
10+
import springfox.documentation.builders.RequestHandlerSelectors;
11+
import springfox.documentation.service.ApiInfo;
12+
import springfox.documentation.spi.DocumentationType;
13+
import springfox.documentation.spring.web.plugins.Docket;
14+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
15+
16+
@EnableSwagger2
17+
@SpringBootApplication
18+
public class MainServerApplicant extends WebMvcConfigurerAdapter
19+
{
20+
21+
public static void main(String[] args)
22+
{
23+
SpringApplication.run(MainServerApplicant.class, args);
24+
}
25+
26+
27+
@Override
28+
public void addInterceptors(InterceptorRegistry registry)
29+
{
30+
registry.addInterceptor(new LoggingInterceptor()).addPathPatterns("/**");
31+
}
32+
33+
34+
@Bean
35+
public Docket docket()
36+
{
37+
return new Docket(DocumentationType.SWAGGER_2)
38+
.select()
39+
.apis(RequestHandlerSelectors.basePackage(getClass().getPackage().getName()))
40+
.paths(PathSelectors.any())
41+
.build()
42+
.apiInfo(generateApiInfo());
43+
}
44+
45+
46+
private ApiInfo generateApiInfo()
47+
{
48+
return new ApiInfo("Build Secure Restful Web service",
49+
"This service to show how create Secure Restful Web service using Spring Boot, MVC and security.", "Version 1.0 - mw",
50+
"urn:tos", "mahmoud.azmy@gmail.com", "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0");
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.mazmy.controller;
2+
3+
import java.util.List;
4+
5+
import javax.validation.Valid;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.web.bind.annotation.DeleteMapping;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.RequestBody;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RequestParam;
16+
import org.springframework.web.bind.annotation.ResponseStatus;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
import com.mazmy.controller.mapper.CarMapper;
20+
import com.mazmy.datatransferobject.CarDTO;
21+
import com.mazmy.domainobject.CarDO;
22+
import com.mazmy.domainobject.ManufacturerDO;
23+
import com.mazmy.domainvalue.ConvertibleValue;
24+
import com.mazmy.domainvalue.EngineType;
25+
import com.mazmy.exception.ConstraintsViolationException;
26+
import com.mazmy.exception.EntityNotFoundException;
27+
import com.mazmy.service.car.CarService;
28+
import com.mazmy.service.manufacturer.ManufacturerService;
29+
30+
/**
31+
*
32+
* @author azmym All operations with a car will be routed by this controller
33+
*/
34+
@RestController
35+
@RequestMapping("v1/cars")
36+
public class CarController {
37+
38+
private final CarService carService;
39+
private final ManufacturerService manufacturerService;
40+
41+
@Autowired
42+
public CarController(final CarService carService, final ManufacturerService manufacturerService) {
43+
this.carService = carService;
44+
this.manufacturerService = manufacturerService;
45+
}
46+
47+
@GetMapping("/{carId}")
48+
public CarDTO getCarById(@Valid @PathVariable long carId) throws EntityNotFoundException {
49+
return CarMapper.makeCarDTO(carService.find(carId));
50+
}
51+
52+
@GetMapping
53+
public List<CarDTO> findByEngineType(@Valid @RequestParam EngineType engineType)
54+
throws ConstraintsViolationException, EntityNotFoundException {
55+
return CarMapper.makeDriverDTOList(carService.findByEngineType(engineType.type()));
56+
}
57+
58+
@GetMapping("/convertible/{convertible}")
59+
public List<CarDTO> findByConvertible(@Valid @PathVariable ConvertibleValue convertible) throws EntityNotFoundException {
60+
return CarMapper.makeDriverDTOList(carService.findByConvertible(convertible.value()));
61+
}
62+
63+
@GetMapping("/manufacturer/{manufacturer}")
64+
public List<CarDTO> findByManufacturer(@Valid @PathVariable String manufacturer) throws EntityNotFoundException {
65+
return CarMapper.makeDriverDTOList(carService.findByManufacturer(manufacturer));
66+
}
67+
68+
/**
69+
* create new CarDo If the Manufacturer wasn't placed, will be set with null
70+
* If Manufacturer Name is placed , will check if exist in DB or not if it's
71+
* not exist in DB will create new one, and if exist will use it
72+
*
73+
* @param carDTO
74+
* @return CarDTO
75+
* @throws ConstraintsViolationException
76+
*/
77+
@PostMapping
78+
@ResponseStatus(HttpStatus.CREATED)
79+
public CarDTO createCar(@Valid @RequestBody CarDTO carDTO) throws ConstraintsViolationException {
80+
CarDO makeCarDO = CarMapper.makeCarDO(carDTO);
81+
ManufacturerDO manufacturer = makeCarDO.getManufacturer();
82+
83+
if (null != manufacturer) {
84+
try {
85+
manufacturer = manufacturerService.findManufacturerName(manufacturer.getManufacturer());
86+
} catch (EntityNotFoundException e1) {
87+
manufacturer = manufacturerService.create(new ManufacturerDO(manufacturer.getManufacturer()));
88+
}
89+
}
90+
makeCarDO.setManufacturer(manufacturer);
91+
return CarMapper.makeCarDTO(carService.create(makeCarDO));
92+
}
93+
94+
@DeleteMapping("/{carId}")
95+
public void deleteCar(@Valid @PathVariable long carId) throws EntityNotFoundException {
96+
carService.delete(carId);
97+
}
98+
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.mazmy.controller;
2+
3+
import com.mazmy.controller.mapper.DriverMapper;
4+
import com.mazmy.datatransferobject.DriverDTO;
5+
import com.mazmy.domainobject.CarDO;
6+
import com.mazmy.domainobject.DriverDO;
7+
import com.mazmy.domainvalue.ConvertibleValue;
8+
import com.mazmy.domainvalue.EngineType;
9+
import com.mazmy.domainvalue.OnlineStatus;
10+
import com.mazmy.exception.CarAlreadyInUseException;
11+
import com.mazmy.exception.ConstraintsViolationException;
12+
import com.mazmy.exception.DriverOfflineException;
13+
import com.mazmy.exception.EntityNotFoundException;
14+
import com.mazmy.service.car.CarService;
15+
import com.mazmy.service.driver.DriverService;
16+
import java.util.List;
17+
import javax.validation.Valid;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.http.HttpStatus;
20+
import org.springframework.web.bind.annotation.DeleteMapping;
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.PathVariable;
23+
import org.springframework.web.bind.annotation.PostMapping;
24+
import org.springframework.web.bind.annotation.PutMapping;
25+
import org.springframework.web.bind.annotation.RequestBody;
26+
import org.springframework.web.bind.annotation.RequestMapping;
27+
import org.springframework.web.bind.annotation.RequestParam;
28+
import org.springframework.web.bind.annotation.ResponseStatus;
29+
import org.springframework.web.bind.annotation.RestController;
30+
31+
/**
32+
* All operations with a driver will be routed by this controller.
33+
* <p/>
34+
*/
35+
@RestController
36+
@RequestMapping("v1/drivers")
37+
public class DriverController
38+
{
39+
40+
private final DriverService driverService;
41+
private final CarService carService;
42+
43+
@Autowired
44+
public DriverController(final DriverService driverService, final CarService carService)
45+
{
46+
this.driverService = driverService;
47+
this.carService = carService;
48+
}
49+
50+
51+
@GetMapping("/{driverId}")
52+
public DriverDTO getDriver(@Valid @PathVariable long driverId) throws EntityNotFoundException
53+
{
54+
return DriverMapper.makeDriverDTO(driverService.find(driverId));
55+
}
56+
57+
/**
58+
* @param driverDTO
59+
* @return
60+
* @throws ConstraintsViolationException
61+
*/
62+
@PostMapping
63+
@ResponseStatus(HttpStatus.CREATED)
64+
public DriverDTO createDriver(@Valid @RequestBody DriverDTO driverDTO) throws ConstraintsViolationException
65+
{
66+
DriverDO driverDO = DriverMapper.makeDriverDO(driverDTO);
67+
return DriverMapper.makeDriverDTO(driverService.create(driverDO));
68+
}
69+
70+
71+
@DeleteMapping("/{driverId}")
72+
public void deleteDriver(@Valid @PathVariable long driverId) throws EntityNotFoundException
73+
{
74+
driverService.delete(driverId);
75+
}
76+
77+
78+
@PutMapping("/{driverId}")
79+
public void updateLocation(
80+
@Valid @PathVariable long driverId, @RequestParam double longitude, @RequestParam double latitude)
81+
throws ConstraintsViolationException, EntityNotFoundException
82+
{
83+
driverService.updateLocation(driverId, longitude, latitude);
84+
}
85+
86+
87+
@GetMapping
88+
public List<DriverDTO> findDrivers(@RequestParam OnlineStatus onlineStatus)
89+
throws ConstraintsViolationException, EntityNotFoundException
90+
{
91+
return DriverMapper.makeDriverDTOList(driverService.find(onlineStatus));
92+
}
93+
94+
/**
95+
* Enable drivers to select a car they are driving with
96+
*
97+
* @param driverId
98+
* @param carId
99+
* @throws EntityNotFoundException
100+
* @throws DriverOfflineException
101+
* @throws CarAlreadyInUseException
102+
*/
103+
@PutMapping("/selectcar")
104+
public void selectCar(@Valid @RequestParam long driverId, @RequestParam long carId)
105+
throws EntityNotFoundException, DriverOfflineException, CarAlreadyInUseException {
106+
CarDO carDO = carService.find(carId);
107+
driverService.selectCar(driverId, carDO);
108+
}
109+
110+
/**
111+
* Enable drivers to deselect a car
112+
*
113+
* @param driverId
114+
* @throws EntityNotFoundException
115+
*/
116+
@PutMapping("/deselectcar")
117+
public void deSelectCar(@Valid @RequestParam long driverId) throws EntityNotFoundException{
118+
driverService.deSelectCar(driverId);
119+
}
120+
121+
122+
@GetMapping("/filter")
123+
public List<DriverDTO> filter( @RequestParam(required = false) EngineType engineType, @RequestParam(required = false) ConvertibleValue convertible, @RequestParam(required = false) String licensePlate){
124+
String engine = (null == engineType) ? null : engineType.type();
125+
Boolean convertibleState = (null == convertible) ? null : convertible.value();
126+
CarDO carDO = new CarDO(licensePlate, 0, convertibleState, 0, engine, null);
127+
return DriverMapper.makeDriverDTOList(driverService.filter(carDO));
128+
}
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.mazmy.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import springfox.documentation.annotations.ApiIgnore;
6+
7+
@Controller
8+
@ApiIgnore
9+
public class HomeController
10+
{
11+
12+
@RequestMapping("/")
13+
public String home()
14+
{
15+
return "redirect:swagger-ui.html";
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.mazmy.controller;
2+
3+
import javax.validation.Valid;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestBody;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestParam;
13+
import org.springframework.web.bind.annotation.ResponseStatus;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
import com.mazmy.controller.mapper.ManufacturerMapper;
17+
import com.mazmy.datatransferobject.ManufacturerDTO;
18+
import com.mazmy.domainobject.ManufacturerDO;
19+
import com.mazmy.exception.ConstraintsViolationException;
20+
import com.mazmy.exception.EntityNotFoundException;
21+
import com.mazmy.service.manufacturer.ManufacturerService;
22+
23+
/**
24+
*
25+
* @author azmym
26+
* All operations with a manufacturers will be routed by this controller
27+
*/
28+
@RestController
29+
@RequestMapping("v1/manufacturers")
30+
public class ManufacturerController {
31+
32+
private final ManufacturerService manufacturerService;
33+
34+
@Autowired
35+
public ManufacturerController(final ManufacturerService manufacturerService) {
36+
this.manufacturerService = manufacturerService;
37+
}
38+
39+
/**
40+
* find Manufacturer By id
41+
* @param manufacturerId
42+
* @return ManufacturerDTO
43+
* @throws EntityNotFoundException
44+
*/
45+
@GetMapping("/{manufacturerId}")
46+
public ManufacturerDTO getManufacturerById(@Valid @PathVariable long manufacturerId) throws EntityNotFoundException {
47+
return ManufacturerMapper.makeManufacturerDTO(manufacturerService.find(manufacturerId));
48+
}
49+
50+
/**
51+
* find Manufacturer by name
52+
* @param manufacturerName
53+
* @return ManufacturerDTO
54+
* @throws EntityNotFoundException
55+
*/
56+
@GetMapping
57+
public ManufacturerDTO getManufacturerByName(@Valid @RequestParam String manufacturerName) throws EntityNotFoundException {
58+
return ManufacturerMapper.makeManufacturerDTO(manufacturerService.findManufacturerName(manufacturerName));
59+
}
60+
61+
/**
62+
* create new CarDo
63+
* @param manufacturerDTO
64+
* @return manufacturerDTO
65+
* @throws ConstraintsViolationException
66+
*/
67+
@PostMapping
68+
@ResponseStatus(HttpStatus.CREATED)
69+
public ManufacturerDTO createManufacturer(@Valid @RequestBody ManufacturerDTO manufacturerDTO) throws ConstraintsViolationException {
70+
ManufacturerDO makeManufacturerDO = ManufacturerMapper.makeManufacturerDO(manufacturerDTO);
71+
return ManufacturerMapper.makeManufacturerDTO(manufacturerService.create(makeManufacturerDO));
72+
}
73+
}

0 commit comments

Comments
 (0)