Skip to content
20 changes: 16 additions & 4 deletions src/main/java/com/example/AKTours/model/entity/City.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package com.example.AKTours.model.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name = "Cities")
@Data
@AllArgsConstructor
@Builder
@Data
@Entity
@JsonIgnoreProperties({"hotels", "airports"})
@NoArgsConstructor
@Table(name = "Cities")
public class City {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name="city_name")
@Column(name = "city_name")
private String name;

@OneToMany(cascade = CascadeType.ALL)
Expand All @@ -29,5 +33,13 @@ public class City {
@JoinColumn(name = "city_id")
private Set<Airport> airports;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "country_id")
private Country country;


public City(String name, Country country) {
this.name = name;
this.country = country;
}
}
12 changes: 10 additions & 2 deletions src/main/java/com/example/AKTours/repository/CityRepository.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.example.AKTours.repository;


import com.example.AKTours.model.entity.City;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CityRepository extends CrudRepository<City, Long> {

City findCityByName(String name);
public City findCityByName(String name);

public List<City> findCitiesByCountryName(String name);

@Query(value = "SELECT *FROM cities c JOIN countries count ON c.country_id = count.id JOIN continents cont ON count.continent_id = cont.continent_id WHERE cont.continent_name=?1", nativeQuery = true)
public List<City> findCitiesByContinentName(String name);
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,74 @@
package com.example.AKTours.web.controllers;

import com.example.AKTours.model.entity.City;
import com.example.AKTours.web.exceptions.EntityNotFoundException;
import com.example.AKTours.web.service.CityService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.*;

import java.util.List;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Log4j2
@Api(value = "City Management System")
@Controller
@RequestMapping(value = {"/", "/cities"})
@CrossOrigin(origins = "http://localhost:4200")
@Log4j2
@RequestMapping(value = {"/cities"})
public class CityController {

private final CityService cityService;

@Autowired
public CityController(CityService cityService) {
this.cityService = cityService;
}

@ApiOperation(value = "Shows all cities", response = List.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully found cities")})
@RequestMapping(method = RequestMethod.GET)
public String cities(Model model) {
model.addAttribute("cities", cityService.findAll());
return "cities";
public ResponseEntity<Object> allCities() {
return new ResponseEntity<>(cityService.findAll(), HttpStatus.OK);
}

@ApiOperation(value = "Displays cities by name", response = City.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully found cities")})
@RequestMapping(value = "/name/{name}", method = RequestMethod.GET)
public String findByCityName(Model model,
@ApiParam(value = "Name of the city", required = true)
@PathVariable("name") String name) throws Exception {
public ResponseEntity<Object> findByCityName(
@ApiParam(value = "Name of the city", required = true)
@PathVariable("name") String name) throws Exception {
log.info("Invoke findByCityName method");
Model cityName = model.addAttribute("cityName", cityService.findCityByName(name));
return "cityName";
return new ResponseEntity<>(cityService.findCityByName(name), HttpStatus.OK);
}

@ApiOperation(value = "Displays cities by continent name", response = City.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully found cities")})
@RequestMapping(value = "/byContinentName/{name}", method = RequestMethod.GET)
public ResponseEntity<Object> findCitiesByContinentName(
@ApiParam(value = "Name of the continent", required = true)
@PathVariable("name") String name) throws EntityNotFoundException {
log.info("Invoke findCitiesByContinentName method");
return new ResponseEntity<>(cityService.findCityByContinentName(name), HttpStatus.OK);
}

@ApiOperation(value = "Displays cities by country name", response = City.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully found cities")})
@RequestMapping(value = "/byCountryName/{name}", method = RequestMethod.GET)
public ResponseEntity<Object> findCitiesByCountryName(
@ApiParam(value = "Name of the continent", required = true)
@PathVariable("name") String name) throws EntityNotFoundException {
log.info("Invoke findCitiesByCountryName method");
return new ResponseEntity<>(cityService.findCityByCountryName(name), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
@RestController
@Api(value = "Trips Management System")
@CrossOrigin(origins = "http://localhost:4200")

public class TripController {
private final TripService tripService;

Expand All @@ -45,12 +44,13 @@ public ResponseEntity<Object> findAllTrips() throws EntityNotFoundException {
@ApiResponse(code = 200, message = "Successfully found trips")})
@RequestMapping(value = "/tripsByHotel/{name}", method = RequestMethod.GET)
public ResponseEntity<List> findTripsByHotelName(
@ApiParam(value = "Name of the hotel", required = true)
@PathVariable("name") String name) throws EntityNotFoundException {
@ApiParam(value = "Name of the hotel", required = true)
@PathVariable("name") String name) throws EntityNotFoundException {
log.info("Invoke findTripsByHotelName method");
return new ResponseEntity<>(tripService.findTripByHotelName(name), HttpStatus.OK);

}

@ApiOperation(value = "Displays trips by city name", response = Trip.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully found trips")})
Expand All @@ -59,8 +59,13 @@ public ResponseEntity<List> findTripsByCityName(
@ApiParam(value = "Name of the city", required = true)
@PathVariable("name") String name) throws EntityNotFoundException {
log.info("Invoke findTripsByCityName method");
return new ResponseEntity<>(tripService.findTripByCityName(name), HttpStatus.OK);
if (name.equals("Select")) {
return new ResponseEntity<>(tripService.findAllTrips(), HttpStatus.OK);
} else {
return new ResponseEntity<>(tripService.findTripByCityName(name), HttpStatus.OK);
}
}

@ApiOperation(value = "Displays trips by country name", response = Trip.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully found trips")})
Expand All @@ -69,11 +74,12 @@ public ResponseEntity<List> findTripsByCountryName(
@ApiParam(value = "Name of the country", required = true)
@PathVariable("name") String name) throws EntityNotFoundException {
log.info("Invoke findTripsByCountryName method");
if(name.equals("Select")){
return new ResponseEntity<>(tripService.findAllTrips(),HttpStatus.OK);
}else{
return new ResponseEntity<>(tripService.findTripByCountryName(name), HttpStatus.OK);
}}
if (name.equals("Select")) {
return new ResponseEntity<>(tripService.findAllTrips(), HttpStatus.OK);
} else {
return new ResponseEntity<>(tripService.findTripByCountryName(name), HttpStatus.OK);
}
}

@ApiOperation(value = "Displays trips by continent name", response = Trip.class)
@ApiResponses(value = {
Expand All @@ -90,17 +96,17 @@ public ResponseEntity<List> findTripsByContinentName(
}
}

@ApiOperation(value = "Displays trips by date", response = Trip.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully found trips")})
@RequestMapping(value = "/tripsByDate/{date}", method = RequestMethod.GET)
public ResponseEntity<List> findTripsByContinentName (
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@ApiParam(value = "Date of the trip", required = true)
@PathVariable("date") LocalDate date) throws EntityNotFoundException {
log.info("Invoke findTripsByContinentName method");
return new ResponseEntity<>(tripService.findTripByDate(date), HttpStatus.OK);
}
@ApiOperation(value = "Displays trips by date", response = Trip.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully found trips")})
@RequestMapping(value = "/tripsByDate/{date}", method = RequestMethod.GET)
public ResponseEntity<List> findTripsByContinentName(
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@ApiParam(value = "Date of the trip", required = true)
@PathVariable("date") LocalDate date) throws EntityNotFoundException {
log.info("Invoke findTripsByContinentName method");
return new ResponseEntity<>(tripService.findTripByDate(date), HttpStatus.OK);
}


@ApiOperation(value = "Displays trips by price", response = Trip.class)
Expand All @@ -109,9 +115,9 @@ public ResponseEntity<List> findTripsByContinentName (
@RequestMapping(value = "/tripsByPrice/{price}", method = RequestMethod.GET)
public ResponseEntity<List> findTripsByContinentName(
@ApiParam(value = "Price of the trip", required = true)
@PathVariable("price")BigDecimal price) throws EntityNotFoundException {
@PathVariable("price") BigDecimal price) throws EntityNotFoundException {
log.info("Invoke findTripsByContinentName method");
return new ResponseEntity<>(tripService.findTripByPrice(price), HttpStatus.OK);
}
}
}

31 changes: 25 additions & 6 deletions src/main/java/com/example/AKTours/web/service/CityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,52 @@

import com.example.AKTours.model.entity.City;
import com.example.AKTours.repository.CityRepository;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import com.example.AKTours.web.exceptions.EntityNotFoundException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Log4j2
@Service
public class CityService {

@Autowired
private final CityRepository cityRepository;

public CityService(CityRepository cityRepository) {
this.cityRepository = cityRepository;
}

public List<City> findAll() {
log.info("Invoke city repisitory findAll ");
log.info("Invoke CityRepisitory findAll ");
List<City> collect = StreamSupport.stream(cityRepository.findAll().spliterator(), false)
.collect(Collectors.toList());
return collect;

}

public City findCityByName(String name) {
log.info("Invoke city repository using" + name);
log.info("Invoke CityRepository findCityByName using " + name);
return cityRepository.findCityByName(name);
}

public List<City> findCityByCountryName(String name) throws EntityNotFoundException {
log.info("Invoke CityRepository findCityByCoutryName using " + name);
if (!cityRepository.findCitiesByCountryName(name).isEmpty()) {
return cityRepository.findCitiesByCountryName(name);
} else {
throw new EntityNotFoundException("Not found any cities for country " + name);
}
}

public List<City> findCityByContinentName(String name) throws EntityNotFoundException {
log.info("Invoke CityRepository findCityByContinentName using " + name);
if (!cityRepository.findCitiesByContinentName(name).isEmpty()) {
return cityRepository.findCitiesByContinentName(name);
} else {
throw new EntityNotFoundException("Not found any cities for continent " + name);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package com.example.AKTours.web.service;

import com.example.AKTours.model.entity.Country;
import com.example.AKTours.model.entity.Hotel;
import com.example.AKTours.repository.CountryRepository;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

@Log4j2
@Service
public class CountryService {

// @Autowired
private final CountryRepository countryRepository;

public CountryService(CountryRepository countryRepository) {
Expand All @@ -30,10 +27,10 @@ public List<Country> findAll() {

public Country findByCountryName(String name) {
log.info("Invoke CountryRepository findByCountryName using " + name);

return countryRepository.findCountryByName(name);
}
public List<Country> findCountryByContinentName(String name){

public List<Country> findCountryByContinentName(String name) {
log.info("Invoke CountryRepository findCountryByContinent_name using " + name);
return StreamSupport
.stream(countryRepository.findCountryByContinent_Name(name).spliterator(), false)
Expand Down
Loading