├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── src ├── main │ ├── java │ │ └── com │ │ │ └── datajpa │ │ │ └── relationship │ │ │ ├── dto │ │ │ ├── requestDto │ │ │ │ ├── CityRequestDto.java │ │ │ │ ├── CategoryRequestDto.java │ │ │ │ ├── AuthorRequestDto.java │ │ │ │ ├── ZipcodeRequestDto.java │ │ │ │ └── BookRequestDto.java │ │ │ ├── responseDto │ │ │ │ ├── CategoryResponseDto.java │ │ │ │ ├── BookResponseDto.java │ │ │ │ └── AuthorResponseDto.java │ │ │ └── mapper.java │ │ │ ├── repository │ │ │ ├── BookRepository.java │ │ │ ├── CityRepository.java │ │ │ ├── AuthorRepository.java │ │ │ ├── ZipcodeRepository.java │ │ │ └── CategoryRepository.java │ │ │ ├── RelationshipApplication.java │ │ │ ├── model │ │ │ ├── City.java │ │ │ ├── Zipcode.java │ │ │ ├── Category.java │ │ │ ├── Author.java │ │ │ └── Book.java │ │ │ ├── service │ │ │ ├── CityService.java │ │ │ ├── ZipcodeService.java │ │ │ ├── CategoryService.java │ │ │ ├── AuthorService.java │ │ │ ├── BookService.java │ │ │ ├── CityServiceImpl.java │ │ │ ├── CategoryServiceImpl.java │ │ │ ├── ZipcodeServiceImpl.java │ │ │ ├── AuthorServiceImpl.java │ │ │ └── BookServiceImpl.java │ │ │ └── controller │ │ │ ├── CityController.java │ │ │ ├── CategoryController.java │ │ │ ├── ZipcodeController.java │ │ │ ├── AuthorController.java │ │ │ └── BookController.java │ └── resources │ │ └── application.properties └── test │ └── java │ └── com │ └── datajpa │ └── relationship │ └── RelationshipApplicationTests.java ├── .gitignore ├── pom.xml ├── mvnw.cmd └── mvnw /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ErgunAhmet/DataJpaYt/HEAD/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/dto/requestDto/CityRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.dto.requestDto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class CityRequestDto { 7 | private String name; 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/dto/requestDto/CategoryRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.dto.requestDto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class CategoryRequestDto { 7 | private String name; 8 | } 9 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar 3 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/dto/requestDto/AuthorRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.dto.requestDto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class AuthorRequestDto { 7 | private String name; 8 | private Long zipcodeId; 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/dto/requestDto/ZipcodeRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.dto.requestDto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class ZipcodeRequestDto { 7 | private String name; 8 | private Long cityId; 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/dto/requestDto/BookRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.dto.requestDto; 2 | 3 | import lombok.Data; 4 | 5 | import java.util.List; 6 | 7 | @Data 8 | public class BookRequestDto { 9 | private String name; 10 | private List authorIds; 11 | private Long categoryId; 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/dto/responseDto/CategoryResponseDto.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.dto.responseDto; 2 | 3 | import lombok.Data; 4 | 5 | import java.util.List; 6 | 7 | @Data 8 | public class CategoryResponseDto { 9 | private Long id; 10 | private String name; 11 | private List bookNames; 12 | } 13 | -------------------------------------------------------------------------------- /src/test/java/com/datajpa/relationship/RelationshipApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class RelationshipApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/dto/responseDto/BookResponseDto.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.dto.responseDto; 2 | 3 | import lombok.Data; 4 | 5 | import java.util.List; 6 | 7 | @Data 8 | public class BookResponseDto { 9 | private Long id; 10 | private String name; 11 | private List authorNames; 12 | private String categoryName; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/dto/responseDto/AuthorResponseDto.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.dto.responseDto; 2 | 3 | import lombok.Data; 4 | 5 | import java.util.List; 6 | 7 | @Data 8 | public class AuthorResponseDto { 9 | private Long id; 10 | private String name; 11 | private List bookNames; 12 | private String zipcodeName; 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/repository/BookRepository.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.repository; 2 | 3 | import com.datajpa.relationship.model.Book; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface BookRepository extends CrudRepository { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/repository/CityRepository.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.repository; 2 | 3 | import com.datajpa.relationship.model.City; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface CityRepository extends CrudRepository { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/repository/AuthorRepository.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.repository; 2 | 3 | import com.datajpa.relationship.model.Author; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface AuthorRepository extends CrudRepository { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/repository/ZipcodeRepository.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.repository; 2 | 3 | import com.datajpa.relationship.model.Zipcode; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface ZipcodeRepository extends CrudRepository { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/repository/CategoryRepository.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.repository; 2 | 3 | import com.datajpa.relationship.model.Category; 4 | import org.springframework.data.repository.CrudRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface CategoryRepository extends CrudRepository { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/RelationshipApplication.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class RelationshipApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(RelationshipApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/model/City.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.model; 2 | 3 | import lombok.Data; 4 | import lombok.NoArgsConstructor; 5 | 6 | import javax.persistence.*; 7 | 8 | @Entity 9 | @Data 10 | @NoArgsConstructor 11 | @Table(name = "City") 12 | public class City { 13 | @Id 14 | @GeneratedValue(strategy = GenerationType.AUTO) 15 | private Long id; 16 | private String name; 17 | 18 | public City(String name) { 19 | this.name = name; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8080 2 | 3 | spring.h2.console.enabled=true 4 | spring.h2.console.path=/h2 5 | 6 | spring.datasource.url=jdbc:h2:mem:memDb;DB_CLOSE_DELAY=-1 7 | spring.datasource.driverClassName=org.h2.Driver 8 | spring.datasource.username=sa 9 | spring.datasource.password= 10 | 11 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 12 | spring.jpa.hibernate.ddl-auto=create-drop 13 | spring.jpa.show-sql=true 14 | spring.jpa.properties.hibernate.format_sql=true 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/service/CityService.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.service; 2 | 3 | import com.datajpa.relationship.dto.requestDto.CityRequestDto; 4 | import com.datajpa.relationship.model.City; 5 | import org.springframework.stereotype.Service; 6 | 7 | import java.util.List; 8 | 9 | @Service 10 | public interface CityService { 11 | public City addCity(CityRequestDto cityRequestDto); 12 | public List getCities(); 13 | public City getCity(Long cityId); 14 | public City deleteCity(Long cityId); 15 | public City editCity(Long cityId, CityRequestDto cityRequestDto); 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/model/Zipcode.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.model; 2 | 3 | import lombok.Data; 4 | import lombok.NoArgsConstructor; 5 | 6 | import javax.persistence.*; 7 | 8 | @Entity 9 | @Data 10 | @NoArgsConstructor 11 | @Table(name = "Zipcode") 12 | public class Zipcode { 13 | @Id 14 | @GeneratedValue(strategy = GenerationType.AUTO) 15 | private Long id; 16 | private String name; 17 | @OneToOne(cascade = CascadeType.ALL) 18 | @JoinColumn(name = "city_id") 19 | private City city; 20 | 21 | public Zipcode(String name, City city) { 22 | this.name = name; 23 | this.city = city; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/service/ZipcodeService.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.service; 2 | 3 | import com.datajpa.relationship.dto.requestDto.ZipcodeRequestDto; 4 | import com.datajpa.relationship.model.Zipcode; 5 | import org.springframework.stereotype.Service; 6 | 7 | import java.util.List; 8 | 9 | @Service 10 | public interface ZipcodeService { 11 | public Zipcode addZipcode(ZipcodeRequestDto zipcodeRequestDto); 12 | public List getZipcodes(); 13 | public Zipcode getZipcode(Long zipcodeId); 14 | public Zipcode deleteZipcode(Long zipcodeId); 15 | public Zipcode editZipcode(Long zipcodeId, ZipcodeRequestDto zipcodeRequestDto); 16 | public Zipcode addCityToZipcode(Long zipcodeId, Long cityId); 17 | public Zipcode removeCityFromZipcode(Long zipcodeId); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/service/CategoryService.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.service; 2 | 3 | import com.datajpa.relationship.dto.requestDto.CategoryRequestDto; 4 | import com.datajpa.relationship.dto.responseDto.CategoryResponseDto; 5 | import com.datajpa.relationship.model.Category; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.List; 9 | 10 | @Service 11 | public interface CategoryService { 12 | public Category getCategory(Long categoryId); 13 | public CategoryResponseDto addCategory(CategoryRequestDto categoryRequestDto); 14 | public CategoryResponseDto getCategoryById(Long categoryId); 15 | public List getCategories(); 16 | public CategoryResponseDto deleteCategory(Long categoryId); 17 | public CategoryResponseDto editCategory(Long categoryId, CategoryRequestDto categoryRequestDto); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/model/Category.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.model; 2 | 3 | import lombok.Data; 4 | import lombok.NoArgsConstructor; 5 | 6 | import javax.persistence.*; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | @Entity 11 | @Data 12 | @NoArgsConstructor 13 | @Table(name = "Category") 14 | public class Category { 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.AUTO) 17 | private Long id; 18 | private String name; 19 | @OneToMany(mappedBy = "category", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 20 | private List books = new ArrayList<>(); 21 | 22 | public Category(String name, List books) { 23 | this.name = name; 24 | this.books = books; 25 | } 26 | 27 | public void addBook(Book book) { 28 | books.add(book); 29 | } 30 | 31 | public void removeBook(Book book) { 32 | books.remove(book); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/service/AuthorService.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.service; 2 | 3 | import com.datajpa.relationship.dto.requestDto.AuthorRequestDto; 4 | import com.datajpa.relationship.dto.responseDto.AuthorResponseDto; 5 | import com.datajpa.relationship.model.Author; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.List; 9 | 10 | @Service 11 | public interface AuthorService { 12 | public AuthorResponseDto addAuthor(AuthorRequestDto authorRequestDto); 13 | public List getAuthors(); 14 | public AuthorResponseDto getAuthorById(Long authorId); 15 | public Author getAuthor(Long authorId); 16 | public AuthorResponseDto deleteAuthor(Long authorId); 17 | public AuthorResponseDto editAuthor(Long authorId, AuthorRequestDto authorRequestDto); 18 | public AuthorResponseDto addZipcodeToAuthor(Long authorId, Long zipcodeId); 19 | public AuthorResponseDto deleteZipcodeFromAuthor(Long authorId); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/service/BookService.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.service; 2 | 3 | import com.datajpa.relationship.dto.requestDto.BookRequestDto; 4 | import com.datajpa.relationship.dto.responseDto.BookResponseDto; 5 | import com.datajpa.relationship.model.Book; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.List; 9 | 10 | @Service 11 | public interface BookService { 12 | public BookResponseDto addBook(BookRequestDto bookRequestDto); 13 | public BookResponseDto getBookById(Long bookId); 14 | public Book getBook(Long bookId); 15 | public List getBooks(); 16 | public BookResponseDto deleteBook(Long bookId); 17 | public BookResponseDto editBook(Long bookId, BookRequestDto bookRequestDto); 18 | public BookResponseDto addAuthorToBook(Long bookId, Long authorId); 19 | public BookResponseDto deleteAuthorFromBook(Long bookId, Long authorId); 20 | public BookResponseDto addCategoryToBook(Long bookId, Long categoryId); 21 | public BookResponseDto removeCategoryFromBook(Long bookId, Long categoryId); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/model/Author.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.model; 2 | 3 | import lombok.Data; 4 | import lombok.NoArgsConstructor; 5 | 6 | import javax.persistence.*; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | @Entity 11 | @Data 12 | @NoArgsConstructor 13 | @Table(name = "Author") 14 | public class Author { 15 | @Id 16 | @GeneratedValue 17 | private Long id; 18 | private String name; 19 | @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) 20 | @JoinColumn(name = "zipcode_id") 21 | private Zipcode zipcode; 22 | @ManyToMany(mappedBy = "category", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 23 | private List books = new ArrayList<>(); 24 | 25 | public Author(String name, Zipcode zipcode, List books) { 26 | this.name = name; 27 | this.zipcode = zipcode; 28 | this.books = books; 29 | } 30 | 31 | public void addBook(Book book) { 32 | books.add(book); 33 | } 34 | 35 | public void removeBook(Book book) { 36 | books.remove(book); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/model/Book.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.model; 2 | 3 | import lombok.Data; 4 | import lombok.NoArgsConstructor; 5 | 6 | import javax.persistence.*; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | @Entity 11 | @Data 12 | @NoArgsConstructor 13 | @Table(name = "Book") 14 | public class Book { 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.AUTO) 17 | private Long id; 18 | private String name; 19 | @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) 20 | @JoinTable( 21 | name = "book_author", 22 | joinColumns = @JoinColumn(name = "book_id"), 23 | inverseJoinColumns = @JoinColumn(name = "author_id") 24 | ) 25 | private List authors = new ArrayList<>(); 26 | @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) 27 | @JoinColumn(name = "category_id") 28 | private Category category; 29 | 30 | public Book(String name, List authors, Category category) { 31 | this.name = name; 32 | this.authors = authors; 33 | this.category = category; 34 | } 35 | 36 | public void addAuthor(Author author) { 37 | authors.add(author); 38 | } 39 | 40 | public void deleteAuthor(Author author) { 41 | authors.remove(author); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/service/CityServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.service; 2 | 3 | import com.datajpa.relationship.dto.requestDto.CityRequestDto; 4 | import com.datajpa.relationship.model.City; 5 | import com.datajpa.relationship.repository.CityRepository; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import javax.transaction.Transactional; 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | @Service 13 | public class CityServiceImpl implements CityService { 14 | 15 | private final CityRepository cityRepository; 16 | 17 | @Autowired 18 | public CityServiceImpl(CityRepository cityRepository) { 19 | this.cityRepository = cityRepository; 20 | } 21 | 22 | 23 | @Override 24 | public City addCity(CityRequestDto cityRequestDto) { 25 | City city = new City(); 26 | city.setName(cityRequestDto.getName()); 27 | return cityRepository.save(city); 28 | } 29 | 30 | @Override 31 | public List getCities() { 32 | List cities = new ArrayList<>(); 33 | cityRepository.findAll().forEach(cities::add); 34 | return cities; 35 | } 36 | 37 | @Override 38 | public City getCity(Long cityId) { 39 | return cityRepository.findById(cityId).orElseThrow(() -> 40 | new IllegalArgumentException("city with cityId: " + cityId + " could not be found")); 41 | } 42 | 43 | @Override 44 | public City deleteCity(Long cityId) { 45 | City city = getCity(cityId); 46 | cityRepository.delete(city); 47 | return city; 48 | } 49 | 50 | @Transactional 51 | @Override 52 | public City editCity(Long cityId, CityRequestDto cityRequestDto) { 53 | City cityToEdit = getCity(cityId); 54 | cityToEdit.setName(cityRequestDto.getName()); 55 | return cityToEdit; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/controller/CityController.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.controller; 2 | 3 | import com.datajpa.relationship.dto.requestDto.CityRequestDto; 4 | import com.datajpa.relationship.model.City; 5 | import com.datajpa.relationship.service.CityService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.util.List; 12 | 13 | @RestController 14 | @RequestMapping("/city") 15 | public class CityController { 16 | 17 | private final CityService cityService; 18 | 19 | @Autowired 20 | public CityController(CityService cityService) { 21 | this.cityService = cityService; 22 | } 23 | 24 | @PostMapping("/add") 25 | public ResponseEntity addCity(@RequestBody final CityRequestDto cityRequestDto) { 26 | City city = cityService.addCity(cityRequestDto); 27 | return new ResponseEntity<>(city, HttpStatus.OK); 28 | } 29 | 30 | @GetMapping("/get/{id}") 31 | public ResponseEntity getCityById(@PathVariable final Long id) { 32 | City city = cityService.getCity(id); 33 | return new ResponseEntity<>(city, HttpStatus.OK); 34 | } 35 | 36 | @GetMapping("/getall") 37 | public ResponseEntity> getCities() { 38 | List cities = cityService.getCities(); 39 | return new ResponseEntity>(cities, HttpStatus.OK); 40 | } 41 | 42 | @DeleteMapping("/delete/{id}") 43 | public ResponseEntity deleteCity(@PathVariable final Long id) { 44 | City city = cityService.deleteCity(id); 45 | return new ResponseEntity<>(city, HttpStatus.OK); 46 | } 47 | 48 | @PostMapping("/edit/id") 49 | public ResponseEntity editCity(@RequestBody final CityRequestDto cityRequestDto, 50 | @PathVariable final Long id) { 51 | City city = cityService.editCity(id, cityRequestDto); 52 | return new ResponseEntity<>(city, HttpStatus.OK); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.5.6 9 | 10 | 11 | com.datajpa 12 | relationship 13 | 0.0.1-SNAPSHOT 14 | relationship 15 | Demo project for Spring Boot 16 | 17 | 11 18 | 19 | 20 | 21 | io.springfox 22 | springfox-boot-starter 23 | 3.0.0 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-data-jpa 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | 35 | com.h2database 36 | h2 37 | runtime 38 | 39 | 40 | org.projectlombok 41 | lombok 42 | true 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-test 47 | test 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | org.projectlombok 60 | lombok 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/controller/CategoryController.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.controller; 2 | 3 | import com.datajpa.relationship.dto.requestDto.CategoryRequestDto; 4 | import com.datajpa.relationship.dto.responseDto.CategoryResponseDto; 5 | import com.datajpa.relationship.service.CategoryService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.util.List; 12 | 13 | @RestController 14 | @RequestMapping("/category") 15 | public class CategoryController { 16 | 17 | private final CategoryService categoryService; 18 | 19 | @Autowired 20 | public CategoryController(CategoryService categoryService) { 21 | this.categoryService = categoryService; 22 | } 23 | 24 | @PostMapping("/add") 25 | public ResponseEntity addCategory( 26 | @RequestBody final CategoryRequestDto categoryRequestDto) { 27 | CategoryResponseDto categoryResponseDto = categoryService.addCategory(categoryRequestDto); 28 | return new ResponseEntity<>(categoryResponseDto, HttpStatus.OK); 29 | } 30 | 31 | @GetMapping("/get/{id}") 32 | public ResponseEntity getCategory(@PathVariable final Long id) { 33 | CategoryResponseDto categoryResponseDto = categoryService.getCategoryById(id); 34 | return new ResponseEntity<>(categoryResponseDto, HttpStatus.OK); 35 | } 36 | 37 | @GetMapping("/getAll") 38 | public ResponseEntity> getCategories() { 39 | List categoryResponseDtos = categoryService.getCategories(); 40 | return new ResponseEntity<>(categoryResponseDtos, HttpStatus.OK); 41 | } 42 | 43 | @DeleteMapping("/delete/{id}") 44 | public ResponseEntity deleteCategory(@PathVariable final Long id) { 45 | CategoryResponseDto categoryResponseDto = categoryService.deleteCategory(id); 46 | return new ResponseEntity<>(categoryResponseDto, HttpStatus.OK); 47 | } 48 | 49 | @PostMapping("/edit/{id}") 50 | public ResponseEntity editCategory( 51 | @RequestBody final CategoryRequestDto categoryRequestDto, 52 | @PathVariable final Long id) { 53 | CategoryResponseDto categoryResponseDto = categoryService.editCategory(id, categoryRequestDto); 54 | return new ResponseEntity<>(categoryResponseDto, HttpStatus.OK); 55 | } 56 | 57 | 58 | } 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/service/CategoryServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.service; 2 | 3 | import com.datajpa.relationship.dto.mapper; 4 | import com.datajpa.relationship.dto.requestDto.CategoryRequestDto; 5 | import com.datajpa.relationship.dto.responseDto.CategoryResponseDto; 6 | import com.datajpa.relationship.model.Category; 7 | import com.datajpa.relationship.repository.CategoryRepository; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import javax.transaction.Transactional; 12 | import java.util.List; 13 | import java.util.stream.Collectors; 14 | import java.util.stream.StreamSupport; 15 | 16 | @Service 17 | public class CategoryServiceImpl implements CategoryService { 18 | 19 | private final CategoryRepository categoryRepository; 20 | 21 | @Autowired 22 | public CategoryServiceImpl(CategoryRepository categoryRepository) { 23 | this.categoryRepository = categoryRepository; 24 | } 25 | 26 | 27 | @Override 28 | public Category getCategory(Long categoryId) { 29 | return categoryRepository.findById(categoryId).orElseThrow(() -> 30 | new IllegalArgumentException("could not find category with id: " + categoryId)); 31 | } 32 | 33 | @Override 34 | public CategoryResponseDto addCategory(CategoryRequestDto categoryRequestDto) { 35 | Category category = new Category(); 36 | category.setName(categoryRequestDto.getName()); 37 | categoryRepository.save(category); 38 | return mapper.categoryToCategoryResponseDto(category); 39 | } 40 | 41 | @Override 42 | public CategoryResponseDto getCategoryById(Long categoryId) { 43 | Category category = getCategory(categoryId); 44 | return mapper.categoryToCategoryResponseDto(category); 45 | } 46 | 47 | @Override 48 | public List getCategories() { 49 | List categories = StreamSupport 50 | .stream(categoryRepository.findAll().spliterator(), false) 51 | .collect(Collectors.toList()); 52 | return mapper.categoriesToCategoryResponseDtos(categories); 53 | } 54 | 55 | @Override 56 | public CategoryResponseDto deleteCategory(Long categoryId) { 57 | Category category = getCategory(categoryId); 58 | categoryRepository.delete(category); 59 | return mapper.categoryToCategoryResponseDto(category); 60 | } 61 | 62 | @Transactional 63 | @Override 64 | public CategoryResponseDto editCategory(Long categoryId, CategoryRequestDto categoryRequestDto) { 65 | Category categoryToEdit = getCategory(categoryId); 66 | categoryToEdit.setName(categoryRequestDto.getName()); 67 | return mapper.categoryToCategoryResponseDto(categoryToEdit); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/controller/ZipcodeController.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.controller; 2 | 3 | import com.datajpa.relationship.dto.requestDto.ZipcodeRequestDto; 4 | import com.datajpa.relationship.model.Zipcode; 5 | import com.datajpa.relationship.service.ZipcodeService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import javax.annotation.PostConstruct; 12 | import java.util.List; 13 | 14 | @RestController 15 | @RequestMapping("/zipcode") 16 | public class ZipcodeController { 17 | 18 | private final ZipcodeService zipcodeService; 19 | 20 | @Autowired 21 | public ZipcodeController(ZipcodeService zipcodeService) { 22 | this.zipcodeService = zipcodeService; 23 | } 24 | 25 | @PostMapping("/add") 26 | public ResponseEntity addZipcode(@RequestBody final ZipcodeRequestDto zipcodeRequestDto) { 27 | Zipcode zipcode = zipcodeService.addZipcode(zipcodeRequestDto); 28 | return new ResponseEntity<>(zipcode, HttpStatus.OK); 29 | } 30 | 31 | @GetMapping("/get/{id}") 32 | public ResponseEntity getZipcode(@PathVariable final Long id) { 33 | Zipcode zipcode = zipcodeService.getZipcode(id); 34 | return new ResponseEntity<>(zipcode, HttpStatus.OK); 35 | } 36 | 37 | @GetMapping("/getAll") 38 | public ResponseEntity> getZipcodes() { 39 | List zipcodes = zipcodeService.getZipcodes(); 40 | return new ResponseEntity<>(zipcodes, HttpStatus.OK); 41 | } 42 | 43 | @DeleteMapping("/delete/{id}") 44 | public ResponseEntity deleteZipcode(@PathVariable final Long id) { 45 | Zipcode zipcode = zipcodeService.deleteZipcode(id); 46 | return new ResponseEntity<>(zipcode, HttpStatus.OK); 47 | } 48 | 49 | @PostMapping("/edit/{id}") 50 | public ResponseEntity editZipcode(@RequestBody final ZipcodeRequestDto zipcodeRequestDto, 51 | @PathVariable final Long id) { 52 | Zipcode zipcode = zipcodeService.editZipcode(id, zipcodeRequestDto); 53 | return new ResponseEntity<>(zipcode, HttpStatus.OK); 54 | } 55 | 56 | @PostMapping("/addCity/{cityId}/toZipcode/{zipcodeId}") 57 | public ResponseEntity addCity(@PathVariable final Long cityId, 58 | @PathVariable final Long zipcodeId) { 59 | Zipcode zipcode = zipcodeService.addCityToZipcode(zipcodeId, cityId); 60 | return new ResponseEntity<>(zipcode, HttpStatus.OK); 61 | } 62 | 63 | @PostMapping("/deleteCity/{zipcodeId}") 64 | public ResponseEntity deleteCity(@PathVariable final Long zipcodeId) { 65 | Zipcode zipcode = zipcodeService.removeCityFromZipcode(zipcodeId); 66 | return new ResponseEntity<>(zipcode, HttpStatus.OK); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/dto/mapper.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.dto; 2 | 3 | import com.datajpa.relationship.dto.responseDto.AuthorResponseDto; 4 | import com.datajpa.relationship.dto.responseDto.BookResponseDto; 5 | import com.datajpa.relationship.dto.responseDto.CategoryResponseDto; 6 | import com.datajpa.relationship.model.Author; 7 | import com.datajpa.relationship.model.Book; 8 | import com.datajpa.relationship.model.Category; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class mapper { 14 | 15 | public static BookResponseDto bookToBookResponseDto(Book book) { 16 | BookResponseDto bookResponseDto = new BookResponseDto(); 17 | bookResponseDto.setId(book.getId()); 18 | bookResponseDto.setCategoryName(book.getCategory().getName()); 19 | List names = new ArrayList<>(); 20 | List authors = book.getAuthors(); 21 | for (Author author: authors) { 22 | names.add(author.getName()); 23 | } 24 | bookResponseDto.setAuthorNames(names); 25 | return bookResponseDto; 26 | } 27 | 28 | public static List booksToBookResponseDtos(List books) { 29 | List bookResponseDtos = new ArrayList<>(); 30 | for (Book book: books) { 31 | bookResponseDtos.add(bookToBookResponseDto(book)); 32 | } 33 | return bookResponseDtos; 34 | } 35 | 36 | public static AuthorResponseDto authorToAuthorResponseDto(Author author) { 37 | AuthorResponseDto authorResponseDto = new AuthorResponseDto(); 38 | authorResponseDto.setId(author.getId()); 39 | authorResponseDto.setName(author.getName()); 40 | List names = new ArrayList<>(); 41 | List books = author.getBooks(); 42 | for (Book book: books) { 43 | names.add(book.getName()); 44 | } 45 | authorResponseDto.setBookNames(names); 46 | return authorResponseDto; 47 | } 48 | 49 | public static List authorsToAuthorResponseDtos(List authors){ 50 | List authorResponseDtos = new ArrayList<>(); 51 | for (Author author: authors) { 52 | authorResponseDtos.add(authorToAuthorResponseDto(author)); 53 | } 54 | return authorResponseDtos; 55 | } 56 | 57 | public static CategoryResponseDto categoryToCategoryResponseDto(Category category) { 58 | CategoryResponseDto categoryResponseDto = new CategoryResponseDto(); 59 | categoryResponseDto.setId(category.getId()); 60 | categoryResponseDto.setName(category.getName()); 61 | List names = new ArrayList<>(); 62 | List books = category.getBooks(); 63 | for (Book book : books) { 64 | names.add(book.getName()); 65 | } 66 | categoryResponseDto.setBookNames(names); 67 | return categoryResponseDto; 68 | } 69 | 70 | public static List categoriesToCategoryResponseDtos(List categories) { 71 | List categoryResponseDtos = new ArrayList<>(); 72 | for (Category category: categories) { 73 | categoryResponseDtos.add(categoryToCategoryResponseDto(category)); 74 | } 75 | return categoryResponseDtos; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/controller/AuthorController.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.controller; 2 | 3 | import com.datajpa.relationship.dto.requestDto.AuthorRequestDto; 4 | import com.datajpa.relationship.dto.responseDto.AuthorResponseDto; 5 | import com.datajpa.relationship.service.AuthorService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.util.List; 12 | 13 | @RestController 14 | @RequestMapping("/author") 15 | public class AuthorController { 16 | 17 | private final AuthorService authorService; 18 | 19 | @Autowired 20 | public AuthorController(AuthorService authorService) { 21 | this.authorService = authorService; 22 | } 23 | 24 | @PostMapping("/addAuthor") 25 | public ResponseEntity addAuthor( 26 | @RequestBody final AuthorRequestDto authorRequestDto) { 27 | AuthorResponseDto authorResponseDto = authorService.addAuthor(authorRequestDto); 28 | return new ResponseEntity<>(authorResponseDto, HttpStatus.OK); 29 | } 30 | 31 | @GetMapping("/get/{id}") 32 | public ResponseEntity getAuthor(@PathVariable final Long id) { 33 | AuthorResponseDto authorResponseDto = authorService.getAuthorById(id); 34 | return new ResponseEntity<>(authorResponseDto, HttpStatus.OK); 35 | } 36 | 37 | @GetMapping("/getAll") 38 | public ResponseEntity> getAuthors() { 39 | List authorResponseDtos = authorService.getAuthors(); 40 | return new ResponseEntity<>(authorResponseDtos, HttpStatus.OK); 41 | } 42 | 43 | @DeleteMapping("/delete/{id}") 44 | public ResponseEntity deleteAuthor(@PathVariable final Long id) { 45 | AuthorResponseDto authorResponseDto = authorService.deleteAuthor(id); 46 | return new ResponseEntity<>(authorResponseDto, HttpStatus.OK); 47 | } 48 | 49 | @PostMapping("/edit/{id}") 50 | private ResponseEntity editAuthor(@PathVariable final Long id, 51 | @RequestBody final AuthorRequestDto authorRequestDto) { 52 | AuthorResponseDto authorResponseDto = authorService.editAuthor(id, authorRequestDto); 53 | return new ResponseEntity<>(authorResponseDto, HttpStatus.OK); 54 | } 55 | 56 | @PostMapping("/addZipcode/{zipcodeId}/to/{authorId}") 57 | private ResponseEntity addZipcode(@PathVariable final Long zipcodeId, 58 | @PathVariable final Long authorId) { 59 | AuthorResponseDto authorResponseDto = authorService.addZipcodeToAuthor(authorId, zipcodeId); 60 | return new ResponseEntity<>(authorResponseDto, HttpStatus.OK); 61 | } 62 | 63 | @PostMapping("/removeZipcode/{id}") 64 | private ResponseEntity removeZipcode(@PathVariable final Long id) { 65 | AuthorResponseDto authorResponseDto = authorService.deleteZipcodeFromAuthor(id); 66 | return new ResponseEntity<>(authorResponseDto, HttpStatus.OK); 67 | } 68 | } 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/service/ZipcodeServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.service; 2 | 3 | import com.datajpa.relationship.dto.requestDto.ZipcodeRequestDto; 4 | import com.datajpa.relationship.model.City; 5 | import com.datajpa.relationship.model.Zipcode; 6 | import com.datajpa.relationship.repository.ZipcodeRepository; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | 10 | import javax.transaction.Transactional; 11 | import java.util.List; 12 | import java.util.Objects; 13 | import java.util.stream.Collectors; 14 | import java.util.stream.StreamSupport; 15 | 16 | @Service 17 | public class ZipcodeServiceImpl implements ZipcodeService { 18 | 19 | private final ZipcodeRepository zipcodeRepository; 20 | private final CityService cityService; 21 | 22 | @Autowired 23 | public ZipcodeServiceImpl(ZipcodeRepository zipcodeRepository, CityService cityService) { 24 | this.zipcodeRepository = zipcodeRepository; 25 | this.cityService = cityService; 26 | } 27 | 28 | @Transactional 29 | @Override 30 | public Zipcode addZipcode(ZipcodeRequestDto zipcodeRequestDto) { 31 | Zipcode zipcode = new Zipcode(); 32 | zipcode.setName(zipcodeRequestDto.getName()); 33 | if (zipcodeRequestDto.getCityId() == null) { 34 | return zipcodeRepository.save(zipcode); 35 | } 36 | City city = cityService.getCity(zipcodeRequestDto.getCityId()); 37 | zipcode.setCity(city); 38 | return zipcodeRepository.save(zipcode); 39 | } 40 | 41 | @Override 42 | public List getZipcodes() { 43 | return StreamSupport 44 | .stream(zipcodeRepository.findAll().spliterator(), false) 45 | .collect(Collectors.toList()); 46 | } 47 | 48 | @Override 49 | public Zipcode getZipcode(Long zipcodeId) { 50 | return zipcodeRepository.findById(zipcodeId).orElseThrow(() -> 51 | new IllegalArgumentException( 52 | "zipcode with id: " + zipcodeId + " could not be found")); 53 | } 54 | 55 | @Override 56 | public Zipcode deleteZipcode(Long zipcodeId) { 57 | Zipcode zipcode = getZipcode(zipcodeId); 58 | zipcodeRepository.delete(zipcode); 59 | return zipcode; 60 | } 61 | 62 | @Transactional 63 | @Override 64 | public Zipcode editZipcode(Long zipcodeId, ZipcodeRequestDto zipcodeRequestDto) { 65 | Zipcode zipcodeToEdit = getZipcode(zipcodeId); 66 | zipcodeToEdit.setName(zipcodeRequestDto.getName()); 67 | if (zipcodeRequestDto.getCityId() != null) { 68 | return zipcodeToEdit; 69 | } 70 | City city = cityService.getCity(zipcodeRequestDto.getCityId()); 71 | zipcodeToEdit.setCity(city); 72 | return zipcodeToEdit; 73 | } 74 | 75 | @Transactional 76 | @Override 77 | public Zipcode addCityToZipcode(Long zipcodeId, Long cityId) { 78 | Zipcode zipcode = getZipcode(zipcodeId); 79 | City city = cityService.getCity(cityId); 80 | if (Objects.nonNull(zipcode.getCity())) { 81 | throw new IllegalArgumentException("zipcode already has a city"); 82 | } 83 | zipcode.setCity(city); 84 | return zipcode; 85 | } 86 | 87 | @Transactional 88 | @Override 89 | public Zipcode removeCityFromZipcode(Long zipcodeId) { 90 | Zipcode zipcode = getZipcode(zipcodeId); 91 | if (!Objects.nonNull(zipcode.getCity())) { 92 | throw new IllegalArgumentException("zipcode does not have a city"); 93 | } 94 | zipcode.setCity(null); 95 | return zipcode; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/service/AuthorServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.service; 2 | 3 | import com.datajpa.relationship.dto.mapper; 4 | import com.datajpa.relationship.dto.requestDto.AuthorRequestDto; 5 | import com.datajpa.relationship.dto.responseDto.AuthorResponseDto; 6 | import com.datajpa.relationship.model.Author; 7 | import com.datajpa.relationship.model.Zipcode; 8 | import com.datajpa.relationship.repository.AuthorRepository; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Service; 11 | 12 | import javax.transaction.Transactional; 13 | import java.util.List; 14 | import java.util.Objects; 15 | import java.util.stream.Collectors; 16 | import java.util.stream.StreamSupport; 17 | 18 | @Service 19 | public class AuthorServiceImpl implements AuthorService { 20 | 21 | private final AuthorRepository authorRepository; 22 | private final ZipcodeService zipcodeService; 23 | 24 | @Autowired 25 | public AuthorServiceImpl(AuthorRepository authorRepository, ZipcodeService zipcodeService) { 26 | this.authorRepository = authorRepository; 27 | this.zipcodeService = zipcodeService; 28 | } 29 | 30 | @Transactional 31 | @Override 32 | public AuthorResponseDto addAuthor(AuthorRequestDto authorRequestDto) { 33 | Author author = new Author(); 34 | author.setName(authorRequestDto.getName()); 35 | if (authorRequestDto.getZipcodeId() == null) { 36 | throw new IllegalArgumentException("author need a zipcode"); 37 | } 38 | Zipcode zipcode = zipcodeService.getZipcode(authorRequestDto.getZipcodeId()); 39 | author.setZipcode(zipcode); 40 | authorRepository.save(author); 41 | return mapper.authorToAuthorResponseDto(author); 42 | } 43 | 44 | @Override 45 | public List getAuthors() { 46 | List authors = StreamSupport 47 | .stream(authorRepository.findAll().spliterator(), false) 48 | .collect(Collectors.toList()); 49 | return mapper.authorsToAuthorResponseDtos(authors); 50 | } 51 | 52 | @Override 53 | public AuthorResponseDto getAuthorById(Long authorId) { 54 | return mapper.authorToAuthorResponseDto(getAuthor(authorId)); 55 | } 56 | 57 | @Override 58 | public Author getAuthor(Long authorId) { 59 | Author author = authorRepository.findById(authorId).orElseThrow(() -> 60 | new IllegalArgumentException( 61 | "author with id: " + authorId + " could not be found")); 62 | return author; 63 | } 64 | 65 | @Override 66 | public AuthorResponseDto deleteAuthor(Long authorId) { 67 | Author author = getAuthor(authorId); 68 | authorRepository.delete(author); 69 | return mapper.authorToAuthorResponseDto(author); 70 | } 71 | 72 | @Transactional 73 | @Override 74 | public AuthorResponseDto editAuthor(Long authorId, AuthorRequestDto authorRequestDto) { 75 | Author authorToEdit = getAuthor(authorId); 76 | authorToEdit.setName(authorRequestDto.getName()); 77 | if (authorRequestDto.getZipcodeId() != null) { 78 | Zipcode zipcode = zipcodeService.getZipcode(authorRequestDto.getZipcodeId()); 79 | authorToEdit.setZipcode(zipcode); 80 | } 81 | return mapper.authorToAuthorResponseDto(authorToEdit); 82 | } 83 | 84 | @Transactional 85 | @Override 86 | public AuthorResponseDto addZipcodeToAuthor(Long authorId, Long zipcodeId) { 87 | Author author = getAuthor(authorId); 88 | Zipcode zipcode = zipcodeService.getZipcode(zipcodeId); 89 | if (Objects.nonNull(author.getZipcode())){ 90 | throw new RuntimeException("author already has a zipcode"); 91 | } 92 | author.setZipcode(zipcode); 93 | return mapper.authorToAuthorResponseDto(author); 94 | } 95 | 96 | @Transactional 97 | @Override 98 | public AuthorResponseDto deleteZipcodeFromAuthor(Long authorId) { 99 | Author author = getAuthor(authorId); 100 | author.setZipcode(null); 101 | return mapper.authorToAuthorResponseDto(author); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/controller/BookController.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.controller; 2 | 3 | import com.datajpa.relationship.dto.requestDto.BookRequestDto; 4 | import com.datajpa.relationship.dto.responseDto.BookResponseDto; 5 | import com.datajpa.relationship.service.BookService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.util.List; 12 | 13 | @RestController 14 | @RequestMapping("/book") 15 | public class BookController { 16 | 17 | private final BookService bookService; 18 | 19 | @Autowired 20 | public BookController(BookService bookService) { 21 | this.bookService = bookService; 22 | } 23 | 24 | @PostMapping("/add") 25 | public ResponseEntity addBook(@RequestBody final BookRequestDto bookRequestDto) { 26 | BookResponseDto bookResponseDto = bookService.addBook(bookRequestDto); 27 | return new ResponseEntity<>(bookResponseDto, HttpStatus.OK); 28 | } 29 | 30 | @GetMapping("/get/{id}") 31 | public ResponseEntity getBook(@PathVariable final Long id) { 32 | BookResponseDto bookResponseDto = bookService.getBookById(id); 33 | return new ResponseEntity<>(bookResponseDto, HttpStatus.OK); 34 | } 35 | 36 | @GetMapping("/getAll") 37 | public ResponseEntity> getBooks() { 38 | List bookResponseDtos = bookService.getBooks(); 39 | return new ResponseEntity<>(bookResponseDtos, HttpStatus.OK); 40 | } 41 | 42 | @DeleteMapping("/delete/{id}") 43 | public ResponseEntity deleteBook(@PathVariable final Long id) { 44 | BookResponseDto bookResponseDto = bookService.deleteBook(id); 45 | return new ResponseEntity<>(bookResponseDto, HttpStatus.OK); 46 | } 47 | 48 | @PostMapping("/edit/{id}") 49 | public ResponseEntity editBook(@RequestBody final BookRequestDto bookRequestDto, 50 | @PathVariable final Long id) { 51 | BookResponseDto bookResponseDto = bookService.editBook(id, bookRequestDto); 52 | return new ResponseEntity<>(bookResponseDto, HttpStatus.OK); 53 | } 54 | 55 | @PostMapping("/addCategory/{categoryId}/to/{bookId}") 56 | public ResponseEntity addCategory(@PathVariable final Long categoryId, 57 | @PathVariable final Long bookId) { 58 | BookResponseDto bookResponseDto = bookService.addCategoryToBook(bookId, categoryId); 59 | return new ResponseEntity<>(bookResponseDto, HttpStatus.OK); 60 | } 61 | 62 | @PostMapping("/removeCategory/{categoryId}/from/{bookId}") 63 | public ResponseEntity removeCategory(@PathVariable final Long categoryId, 64 | @PathVariable final Long bookId) { 65 | BookResponseDto bookResponseDto = bookService.removeCategoryFromBook(bookId, categoryId); 66 | return new ResponseEntity<>(bookResponseDto, HttpStatus.OK); 67 | } 68 | 69 | @PostMapping("/addAuthor/{authorId}/to/{bookId}") 70 | public ResponseEntity addAuthor(@PathVariable final Long authorId, 71 | @PathVariable final Long bookId) { 72 | BookResponseDto bookResponseDto = bookService.addAuthorToBook(bookId, authorId); 73 | return new ResponseEntity<>(bookResponseDto, HttpStatus.OK); 74 | } 75 | 76 | @PostMapping("/removeAuthor/{authorId}/from/{bookId}") 77 | public ResponseEntity removeAuthor(@PathVariable final Long authorId, 78 | @PathVariable final Long bookId) { 79 | BookResponseDto bookResponseDto = bookService.deleteAuthorFromBook(bookId, authorId); 80 | return new ResponseEntity<>(bookResponseDto, HttpStatus.OK); 81 | } 82 | } 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /src/main/java/com/datajpa/relationship/service/BookServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.datajpa.relationship.service; 2 | 3 | import com.datajpa.relationship.dto.mapper; 4 | import com.datajpa.relationship.dto.requestDto.BookRequestDto; 5 | import com.datajpa.relationship.dto.responseDto.BookResponseDto; 6 | import com.datajpa.relationship.model.Author; 7 | import com.datajpa.relationship.model.Book; 8 | import com.datajpa.relationship.model.Category; 9 | import com.datajpa.relationship.repository.BookRepository; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | 13 | import javax.transaction.Transactional; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | import java.util.Objects; 17 | import java.util.stream.Collectors; 18 | import java.util.stream.StreamSupport; 19 | 20 | @Service 21 | public class BookServiceImpl implements BookService { 22 | 23 | private final BookRepository bookRepository; 24 | private final AuthorService authorService; 25 | private final CategoryService categoryService; 26 | 27 | @Autowired 28 | public BookServiceImpl(BookRepository bookRepository, AuthorService authorService, CategoryService categoryService) { 29 | this.bookRepository = bookRepository; 30 | this.authorService = authorService; 31 | this.categoryService = categoryService; 32 | } 33 | 34 | @Transactional 35 | @Override 36 | public BookResponseDto addBook(BookRequestDto bookRequestDto) { 37 | Book book = new Book(); 38 | book.setName(bookRequestDto.getName()); 39 | if (bookRequestDto.getAuthorIds().isEmpty()) { 40 | throw new IllegalArgumentException("you need atleast on author"); 41 | } else { 42 | List authors = new ArrayList(); 43 | for (Long authorId: bookRequestDto.getAuthorIds()) { 44 | Author author = authorService.getAuthor(authorId); 45 | authors.add(author); 46 | } 47 | book.setAuthors(authors); 48 | } 49 | if (bookRequestDto.getCategoryId() == null) { 50 | throw new IllegalArgumentException("book atleast on category"); 51 | } 52 | Category category = categoryService.getCategory(bookRequestDto.getCategoryId()); 53 | book.setCategory(category); 54 | 55 | Book book1 = bookRepository.save(book); 56 | return mapper.bookToBookResponseDto(book1); 57 | } 58 | 59 | @Override 60 | public BookResponseDto getBookById(Long bookId) { 61 | Book book = getBook(bookId); 62 | return mapper.bookToBookResponseDto(book); 63 | } 64 | 65 | @Override 66 | public Book getBook(Long bookId) { 67 | Book book = bookRepository.findById(bookId).orElseThrow(() -> 68 | new IllegalArgumentException("cannot find book with id: " + bookId)); 69 | return book; 70 | } 71 | 72 | @Override 73 | public List getBooks() { 74 | List books = StreamSupport 75 | .stream(bookRepository.findAll().spliterator(), false) 76 | .collect(Collectors.toList()); 77 | return mapper.booksToBookResponseDtos(books); 78 | } 79 | 80 | @Override 81 | public BookResponseDto deleteBook(Long bookId) { 82 | Book book = getBook(bookId); 83 | bookRepository.delete(book); 84 | return mapper.bookToBookResponseDto(book); 85 | } 86 | 87 | @Transactional 88 | @Override 89 | public BookResponseDto editBook(Long bookId, BookRequestDto bookRequestDto) { 90 | Book bookToEdit = getBook(bookId); 91 | bookToEdit.setName(bookRequestDto.getName()); 92 | if (!bookRequestDto.getAuthorIds().isEmpty()){ 93 | List authors = new ArrayList<>(); 94 | for (Long authorId: bookRequestDto.getAuthorIds()) { 95 | Author author = authorService.getAuthor(authorId); 96 | authors.add(author); 97 | } 98 | bookToEdit.setAuthors(authors); 99 | } 100 | if (bookRequestDto.getCategoryId() != null) { 101 | Category category = categoryService.getCategory(bookRequestDto.getCategoryId()); 102 | bookToEdit.setCategory(category); 103 | } 104 | return mapper.bookToBookResponseDto(bookToEdit); 105 | } 106 | 107 | @Override 108 | public BookResponseDto addAuthorToBook(Long bookId, Long authorId) { 109 | Book book = getBook(bookId); 110 | Author author = authorService.getAuthor(authorId); 111 | if (author.getBooks().contains(author)) { 112 | throw new IllegalArgumentException("this author is already assigned to this book"); 113 | } 114 | book.addAuthor(author); 115 | author.addBook(book); 116 | return mapper.bookToBookResponseDto(book); 117 | } 118 | 119 | @Override 120 | public BookResponseDto deleteAuthorFromBook(Long bookId, Long authorId) { 121 | Book book = getBook(bookId); 122 | Author author = authorService.getAuthor(authorId); 123 | if (!(author.getBooks().contains(book))){ 124 | throw new IllegalArgumentException("book does not have this author"); 125 | } 126 | author.removeBook(book); 127 | book.deleteAuthor(author); 128 | return mapper.bookToBookResponseDto(book); 129 | } 130 | 131 | @Override 132 | public BookResponseDto addCategoryToBook(Long bookId, Long categoryId) { 133 | Book book = getBook(bookId); 134 | Category category = categoryService.getCategory(categoryId); 135 | if (Objects.nonNull(book.getCategory())){ 136 | throw new IllegalArgumentException("book already has a catogory"); 137 | } 138 | book.setCategory(category); 139 | category.addBook(book); 140 | return mapper.bookToBookResponseDto(book); 141 | } 142 | 143 | @Override 144 | public BookResponseDto removeCategoryFromBook(Long bookId, Long categoryId) { 145 | Book book = getBook(bookId); 146 | Category category = categoryService.getCategory(categoryId); 147 | if (!(Objects.nonNull(book.getCategory()))){ 148 | throw new IllegalArgumentException("book does not have a category to delete"); 149 | } 150 | book.setCategory(null); 151 | category.removeBook(book); 152 | return mapper.bookToBookResponseDto(book); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM https://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* 50 | if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 124 | 125 | FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( 126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 127 | ) 128 | 129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 131 | if exist %WRAPPER_JAR% ( 132 | if "%MVNW_VERBOSE%" == "true" ( 133 | echo Found %WRAPPER_JAR% 134 | ) 135 | ) else ( 136 | if not "%MVNW_REPOURL%" == "" ( 137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 138 | ) 139 | if "%MVNW_VERBOSE%" == "true" ( 140 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 141 | echo Downloading from: %DOWNLOAD_URL% 142 | ) 143 | 144 | powershell -Command "&{"^ 145 | "$webclient = new-object System.Net.WebClient;"^ 146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ 147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ 148 | "}"^ 149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ 150 | "}" 151 | if "%MVNW_VERBOSE%" == "true" ( 152 | echo Finished downloading %WRAPPER_JAR% 153 | ) 154 | ) 155 | @REM End of extension 156 | 157 | @REM Provide a "standardized" way to retrieve the CLI args that will 158 | @REM work with both Windows and non-Windows executions. 159 | set MAVEN_CMD_LINE_ARGS=%* 160 | 161 | %MAVEN_JAVA_EXE% ^ 162 | %JVM_CONFIG_MAVEN_PROPS% ^ 163 | %MAVEN_OPTS% ^ 164 | %MAVEN_DEBUG_OPTS% ^ 165 | -classpath %WRAPPER_JAR% ^ 166 | "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ 167 | %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 168 | if ERRORLEVEL 1 goto error 169 | goto end 170 | 171 | :error 172 | set ERROR_CODE=1 173 | 174 | :end 175 | @endlocal & set ERROR_CODE=%ERROR_CODE% 176 | 177 | if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost 178 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 179 | if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" 180 | if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" 181 | :skipRcPost 182 | 183 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 184 | if "%MAVEN_BATCH_PAUSE%"=="on" pause 185 | 186 | if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% 187 | 188 | cmd /C exit /B %ERROR_CODE% 189 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # https://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /usr/local/etc/mavenrc ] ; then 40 | . /usr/local/etc/mavenrc 41 | fi 42 | 43 | if [ -f /etc/mavenrc ] ; then 44 | . /etc/mavenrc 45 | fi 46 | 47 | if [ -f "$HOME/.mavenrc" ] ; then 48 | . "$HOME/.mavenrc" 49 | fi 50 | 51 | fi 52 | 53 | # OS specific support. $var _must_ be set to either true or false. 54 | cygwin=false; 55 | darwin=false; 56 | mingw=false 57 | case "`uname`" in 58 | CYGWIN*) cygwin=true ;; 59 | MINGW*) mingw=true;; 60 | Darwin*) darwin=true 61 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 62 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 63 | if [ -z "$JAVA_HOME" ]; then 64 | if [ -x "/usr/libexec/java_home" ]; then 65 | export JAVA_HOME="`/usr/libexec/java_home`" 66 | else 67 | export JAVA_HOME="/Library/Java/Home" 68 | fi 69 | fi 70 | ;; 71 | esac 72 | 73 | if [ -z "$JAVA_HOME" ] ; then 74 | if [ -r /etc/gentoo-release ] ; then 75 | JAVA_HOME=`java-config --jre-home` 76 | fi 77 | fi 78 | 79 | if [ -z "$M2_HOME" ] ; then 80 | ## resolve links - $0 may be a link to maven's home 81 | PRG="$0" 82 | 83 | # need this for relative symlinks 84 | while [ -h "$PRG" ] ; do 85 | ls=`ls -ld "$PRG"` 86 | link=`expr "$ls" : '.*-> \(.*\)$'` 87 | if expr "$link" : '/.*' > /dev/null; then 88 | PRG="$link" 89 | else 90 | PRG="`dirname "$PRG"`/$link" 91 | fi 92 | done 93 | 94 | saveddir=`pwd` 95 | 96 | M2_HOME=`dirname "$PRG"`/.. 97 | 98 | # make it fully qualified 99 | M2_HOME=`cd "$M2_HOME" && pwd` 100 | 101 | cd "$saveddir" 102 | # echo Using m2 at $M2_HOME 103 | fi 104 | 105 | # For Cygwin, ensure paths are in UNIX format before anything is touched 106 | if $cygwin ; then 107 | [ -n "$M2_HOME" ] && 108 | M2_HOME=`cygpath --unix "$M2_HOME"` 109 | [ -n "$JAVA_HOME" ] && 110 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 111 | [ -n "$CLASSPATH" ] && 112 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 113 | fi 114 | 115 | # For Mingw, ensure paths are in UNIX format before anything is touched 116 | if $mingw ; then 117 | [ -n "$M2_HOME" ] && 118 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 119 | [ -n "$JAVA_HOME" ] && 120 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 121 | fi 122 | 123 | if [ -z "$JAVA_HOME" ]; then 124 | javaExecutable="`which javac`" 125 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 126 | # readlink(1) is not available as standard on Solaris 10. 127 | readLink=`which readlink` 128 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 129 | if $darwin ; then 130 | javaHome="`dirname \"$javaExecutable\"`" 131 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 132 | else 133 | javaExecutable="`readlink -f \"$javaExecutable\"`" 134 | fi 135 | javaHome="`dirname \"$javaExecutable\"`" 136 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 137 | JAVA_HOME="$javaHome" 138 | export JAVA_HOME 139 | fi 140 | fi 141 | fi 142 | 143 | if [ -z "$JAVACMD" ] ; then 144 | if [ -n "$JAVA_HOME" ] ; then 145 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 146 | # IBM's JDK on AIX uses strange locations for the executables 147 | JAVACMD="$JAVA_HOME/jre/sh/java" 148 | else 149 | JAVACMD="$JAVA_HOME/bin/java" 150 | fi 151 | else 152 | JAVACMD="`\\unset -f command; \\command -v java`" 153 | fi 154 | fi 155 | 156 | if [ ! -x "$JAVACMD" ] ; then 157 | echo "Error: JAVA_HOME is not defined correctly." >&2 158 | echo " We cannot execute $JAVACMD" >&2 159 | exit 1 160 | fi 161 | 162 | if [ -z "$JAVA_HOME" ] ; then 163 | echo "Warning: JAVA_HOME environment variable is not set." 164 | fi 165 | 166 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 167 | 168 | # traverses directory structure from process work directory to filesystem root 169 | # first directory with .mvn subdirectory is considered project base directory 170 | find_maven_basedir() { 171 | 172 | if [ -z "$1" ] 173 | then 174 | echo "Path not specified to find_maven_basedir" 175 | return 1 176 | fi 177 | 178 | basedir="$1" 179 | wdir="$1" 180 | while [ "$wdir" != '/' ] ; do 181 | if [ -d "$wdir"/.mvn ] ; then 182 | basedir=$wdir 183 | break 184 | fi 185 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 186 | if [ -d "${wdir}" ]; then 187 | wdir=`cd "$wdir/.."; pwd` 188 | fi 189 | # end of workaround 190 | done 191 | echo "${basedir}" 192 | } 193 | 194 | # concatenates all lines of a file 195 | concat_lines() { 196 | if [ -f "$1" ]; then 197 | echo "$(tr -s '\n' ' ' < "$1")" 198 | fi 199 | } 200 | 201 | BASE_DIR=`find_maven_basedir "$(pwd)"` 202 | if [ -z "$BASE_DIR" ]; then 203 | exit 1; 204 | fi 205 | 206 | ########################################################################################## 207 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 208 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 209 | ########################################################################################## 210 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 211 | if [ "$MVNW_VERBOSE" = true ]; then 212 | echo "Found .mvn/wrapper/maven-wrapper.jar" 213 | fi 214 | else 215 | if [ "$MVNW_VERBOSE" = true ]; then 216 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 217 | fi 218 | if [ -n "$MVNW_REPOURL" ]; then 219 | jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 220 | else 221 | jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" 222 | fi 223 | while IFS="=" read key value; do 224 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 225 | esac 226 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 227 | if [ "$MVNW_VERBOSE" = true ]; then 228 | echo "Downloading from: $jarUrl" 229 | fi 230 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 231 | if $cygwin; then 232 | wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` 233 | fi 234 | 235 | if command -v wget > /dev/null; then 236 | if [ "$MVNW_VERBOSE" = true ]; then 237 | echo "Found wget ... using wget" 238 | fi 239 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 240 | wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 241 | else 242 | wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" 243 | fi 244 | elif command -v curl > /dev/null; then 245 | if [ "$MVNW_VERBOSE" = true ]; then 246 | echo "Found curl ... using curl" 247 | fi 248 | if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then 249 | curl -o "$wrapperJarPath" "$jarUrl" -f 250 | else 251 | curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f 252 | fi 253 | 254 | else 255 | if [ "$MVNW_VERBOSE" = true ]; then 256 | echo "Falling back to using Java to download" 257 | fi 258 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 259 | # For Cygwin, switch paths to Windows format before running javac 260 | if $cygwin; then 261 | javaClass=`cygpath --path --windows "$javaClass"` 262 | fi 263 | if [ -e "$javaClass" ]; then 264 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 265 | if [ "$MVNW_VERBOSE" = true ]; then 266 | echo " - Compiling MavenWrapperDownloader.java ..." 267 | fi 268 | # Compiling the Java class 269 | ("$JAVA_HOME/bin/javac" "$javaClass") 270 | fi 271 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 272 | # Running the downloader 273 | if [ "$MVNW_VERBOSE" = true ]; then 274 | echo " - Running MavenWrapperDownloader.java ..." 275 | fi 276 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 277 | fi 278 | fi 279 | fi 280 | fi 281 | ########################################################################################## 282 | # End of extension 283 | ########################################################################################## 284 | 285 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 286 | if [ "$MVNW_VERBOSE" = true ]; then 287 | echo $MAVEN_PROJECTBASEDIR 288 | fi 289 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 290 | 291 | # For Cygwin, switch paths to Windows format before running java 292 | if $cygwin; then 293 | [ -n "$M2_HOME" ] && 294 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 295 | [ -n "$JAVA_HOME" ] && 296 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 297 | [ -n "$CLASSPATH" ] && 298 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 299 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 300 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 301 | fi 302 | 303 | # Provide a "standardized" way to retrieve the CLI args that will 304 | # work with both Windows and non-Windows executions. 305 | MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" 306 | export MAVEN_CMD_LINE_ARGS 307 | 308 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 309 | 310 | exec "$JAVACMD" \ 311 | $MAVEN_OPTS \ 312 | $MAVEN_DEBUG_OPTS \ 313 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 314 | "-Dmaven.home=${M2_HOME}" \ 315 | "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 316 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 317 | --------------------------------------------------------------------------------