├── Documentation ├── 1_ Spring.md ├── 2_ SqlCorsMessageBroker.md ├── 3_ ReflectionHateoasTransaction.md ├── 4_ Security.md ├── 5_ Testing.md ├── 6_ Docker.md ├── 7_Kafka.md ├── 8_SDLC.md └── Others.md ├── Projects ├── Bootcamp-Graduation-Project │ ├── README.md │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── softtech │ │ │ │ └── graduationproject │ │ │ │ ├── GraduationProjectApplication.java │ │ │ │ └── app │ │ │ │ ├── gen │ │ │ │ ├── config │ │ │ │ │ └── SwaggerConfig.java │ │ │ │ ├── dto │ │ │ │ │ └── RestResponse.java │ │ │ │ ├── entity │ │ │ │ │ ├── BaseAdditionalFields.java │ │ │ │ │ ├── BaseEntity.java │ │ │ │ │ └── BaseModel.java │ │ │ │ ├── enums │ │ │ │ │ ├── BaseErrorMessage.java │ │ │ │ │ ├── GenErrorMessage.java │ │ │ │ │ └── GenStatusType.java │ │ │ │ ├── exception │ │ │ │ │ ├── GenCustomizedResponseEntityExceptionHandler.java │ │ │ │ │ └── GenExceptionResponse.java │ │ │ │ ├── exceptions │ │ │ │ │ ├── GenBusinessException.java │ │ │ │ │ ├── IllegalFieldException.java │ │ │ │ │ ├── ItemAlreadyExistsException.java │ │ │ │ │ └── ItemNotFoundException.java │ │ │ │ └── service │ │ │ │ │ └── BaseEntityService.java │ │ │ │ ├── prd │ │ │ │ ├── controller │ │ │ │ │ └── PrdProductController.java │ │ │ │ ├── converter │ │ │ │ │ └── PrdProductMapper.java │ │ │ │ ├── dao │ │ │ │ │ └── PrdProductDao.java │ │ │ │ ├── dto │ │ │ │ │ ├── PrdProductAnalysisRequestDto.java │ │ │ │ │ ├── PrdProductDto.java │ │ │ │ │ ├── PrdProductSaveRequestDto.java │ │ │ │ │ ├── PrdProductUpdateRequestDto.java │ │ │ │ │ └── PrdVatRateDto.java │ │ │ │ ├── entity │ │ │ │ │ └── PrdProduct.java │ │ │ │ ├── enums │ │ │ │ │ └── PrdErrorMessage.java │ │ │ │ └── service │ │ │ │ │ ├── PrdProductService.java │ │ │ │ │ ├── PrdProductUtilityService.java │ │ │ │ │ ├── PrdProductValidationService.java │ │ │ │ │ └── entityservice │ │ │ │ │ └── PrdProductEntityService.java │ │ │ │ ├── sec │ │ │ │ ├── config │ │ │ │ │ └── SecurityConfig.java │ │ │ │ ├── controller │ │ │ │ │ └── AuthenticationController.java │ │ │ │ ├── dto │ │ │ │ │ └── SecLoginRequestDto.java │ │ │ │ ├── enums │ │ │ │ │ └── EnumJwtConstant.java │ │ │ │ ├── security │ │ │ │ │ ├── JwtAuthenticationEntryPoint.java │ │ │ │ │ ├── JwtAuthenticationFilter.java │ │ │ │ │ ├── JwtTokenGenerator.java │ │ │ │ │ ├── JwtUserDetails.java │ │ │ │ │ └── JwtUserDetailsService.java │ │ │ │ └── service │ │ │ │ │ └── AuthenticationService.java │ │ │ │ ├── usr │ │ │ │ ├── controller │ │ │ │ │ └── UsrUserController.java │ │ │ │ ├── converter │ │ │ │ │ └── UsrUserMapper.java │ │ │ │ ├── dao │ │ │ │ │ └── UsrUserDao.java │ │ │ │ ├── dto │ │ │ │ │ ├── UsrUserDto.java │ │ │ │ │ ├── UsrUserFindByIdRequestDto.java │ │ │ │ │ ├── UsrUserSaveRequestDto.java │ │ │ │ │ └── UsrUserUpdateRequestDto.java │ │ │ │ ├── entity │ │ │ │ │ └── UsrUser.java │ │ │ │ ├── enums │ │ │ │ │ └── UsrErrorMessage.java │ │ │ │ └── service │ │ │ │ │ ├── UsrUserService.java │ │ │ │ │ ├── UsrUserValidationService.java │ │ │ │ │ └── entityservice │ │ │ │ │ └── UsrUserEntityService.java │ │ │ │ └── vrt │ │ │ │ ├── controller │ │ │ │ └── VrtVatRateController.java │ │ │ │ ├── converter │ │ │ │ └── VrtVatRateMapper.java │ │ │ │ ├── dao │ │ │ │ └── VrtVatRateDao.java │ │ │ │ ├── dto │ │ │ │ ├── VrtVatRateDto.java │ │ │ │ ├── VrtVatRateSaveRequestDto.java │ │ │ │ └── VrtVatRateUpdateRequestDto.java │ │ │ │ ├── entity │ │ │ │ └── VrtVatRate.java │ │ │ │ ├── enums │ │ │ │ ├── VrtErrorMessage.java │ │ │ │ └── VrtProductType.java │ │ │ │ └── service │ │ │ │ ├── VrtVatRateService.java │ │ │ │ ├── VrtVatRateValidationService.java │ │ │ │ └── entityservice │ │ │ │ └── VrtVatRateEntityService.java │ │ └── resources │ │ │ ├── application.properties │ │ │ └── banner.txt │ │ └── test │ │ └── java │ │ ├── com │ │ └── softtech │ │ │ └── graduationproject │ │ │ ├── GraduationProjectApplicationTests.java │ │ │ └── app │ │ │ ├── BaseTest.java │ │ │ ├── config │ │ │ └── H2TestProfileJPAConfig.java │ │ │ ├── prd │ │ │ ├── controller │ │ │ │ └── PrdProductControllerTest.java │ │ │ └── service │ │ │ │ └── PrdProductServiceTest.java │ │ │ ├── usr │ │ │ ├── controller │ │ │ │ └── UsrUserControllerTest.java │ │ │ └── service │ │ │ │ └── UsrUserServiceTest.java │ │ │ └── vrt │ │ │ ├── controller │ │ │ └── VrtVatRateControllerTest.java │ │ │ └── service │ │ │ └── VrtVatRateServiceTest.java │ │ └── resources │ │ └── application.properties ├── Case1 │ ├── README.md │ └── src │ │ ├── Main.java │ │ ├── entity │ │ ├── BaseHouse.java │ │ ├── House.java │ │ ├── HouseDTO.java │ │ ├── HouseFactory.java │ │ ├── Summerhouse.java │ │ └── Villa.java │ │ └── service │ │ └── HouseService.java ├── Case2 │ ├── README.md │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── softtech │ │ │ │ └── softtechspringboot │ │ │ │ ├── SofttechSpringBootApplication.java │ │ │ │ ├── add │ │ │ │ ├── controller │ │ │ │ │ └── AddAddressController.java │ │ │ │ ├── converter │ │ │ │ │ └── AddAddressMapper.java │ │ │ │ ├── dao │ │ │ │ │ └── AddAddressDao.java │ │ │ │ ├── dto │ │ │ │ │ ├── AddAddressDto.java │ │ │ │ │ └── AddAddressSaveRequestDto.java │ │ │ │ ├── entity │ │ │ │ │ └── AddAddress.java │ │ │ │ ├── enums │ │ │ │ │ └── AddErrorMessage.java │ │ │ │ └── service │ │ │ │ │ ├── AddAddressService.java │ │ │ │ │ └── entityservice │ │ │ │ │ └── AddAddressEntityService.java │ │ │ │ ├── bld │ │ │ │ ├── dao │ │ │ │ │ └── BldBuildingDao.java │ │ │ │ ├── dto │ │ │ │ │ └── BldBuildingDto.java │ │ │ │ └── entity │ │ │ │ │ └── BldBuilding.java │ │ │ │ ├── cnt │ │ │ │ ├── controller │ │ │ │ │ └── CntCountryController.java │ │ │ │ ├── converter │ │ │ │ │ └── CntCountryMapper.java │ │ │ │ ├── dao │ │ │ │ │ └── CntCountryDao.java │ │ │ │ ├── dto │ │ │ │ │ ├── CntCountryDto.java │ │ │ │ │ └── CntCountrySaveRequestDto.java │ │ │ │ ├── entity │ │ │ │ │ └── CntCountry.java │ │ │ │ ├── enums │ │ │ │ │ └── CntErrorMessage.java │ │ │ │ └── service │ │ │ │ │ ├── CntCountryService.java │ │ │ │ │ └── entityservice │ │ │ │ │ └── CntCountryEntityService.java │ │ │ │ ├── cty │ │ │ │ ├── controller │ │ │ │ │ └── CtyCityController.java │ │ │ │ ├── converter │ │ │ │ │ └── CtyCityMapper.java │ │ │ │ ├── dao │ │ │ │ │ └── CtyCityDao.java │ │ │ │ ├── dto │ │ │ │ │ ├── CtyCityDto.java │ │ │ │ │ └── CtyCitySaveRequestDto.java │ │ │ │ ├── entity │ │ │ │ │ └── CtyCity.java │ │ │ │ ├── enums │ │ │ │ │ └── CtyErrorMessage.java │ │ │ │ └── service │ │ │ │ │ ├── CtyCityService.java │ │ │ │ │ └── entityservice │ │ │ │ │ └── CtyCityEntityService.java │ │ │ │ ├── dor │ │ │ │ ├── dao │ │ │ │ │ └── DorDoorDao.java │ │ │ │ ├── dto │ │ │ │ │ └── DorDoorDto.java │ │ │ │ ├── entity │ │ │ │ │ └── DorDoor.java │ │ │ │ └── service │ │ │ │ │ ├── DorDoorService.java │ │ │ │ │ └── entityservice │ │ │ │ │ └── DorDoorEntityService.java │ │ │ │ ├── dst │ │ │ │ ├── controller │ │ │ │ │ └── DstDistrictController.java │ │ │ │ ├── converter │ │ │ │ │ └── DstDistrictMapper.java │ │ │ │ ├── dao │ │ │ │ │ └── DstDistrictDao.java │ │ │ │ ├── dto │ │ │ │ │ ├── DstDistrictDto.java │ │ │ │ │ └── DstDistrictSaveRequestDto.java │ │ │ │ ├── entity │ │ │ │ │ └── DstDistrict.java │ │ │ │ ├── enums │ │ │ │ │ └── DstErrorMessage.java │ │ │ │ └── service │ │ │ │ │ ├── DstDistrictService.java │ │ │ │ │ └── entityservice │ │ │ │ │ └── DstDistrictEntityService.java │ │ │ │ ├── gen │ │ │ │ ├── enums │ │ │ │ │ ├── BaseErrorMessage.java │ │ │ │ │ └── GenErrorMessage.java │ │ │ │ ├── exception │ │ │ │ │ ├── GenCustomizedResponseEntityExceptionHandler.java │ │ │ │ │ └── GenExceptionResponse.java │ │ │ │ ├── exceptions │ │ │ │ │ └── ItemNotFoundException.java │ │ │ │ └── service │ │ │ │ │ └── BaseEntityService.java │ │ │ │ ├── ngh │ │ │ │ ├── controller │ │ │ │ │ └── NghNeighborhoodController.java │ │ │ │ ├── converter │ │ │ │ │ └── NghNeighborhoodMapper.java │ │ │ │ ├── dao │ │ │ │ │ └── NghNeighborhoodDao.java │ │ │ │ ├── dto │ │ │ │ │ ├── NghNeighborhoodDto.java │ │ │ │ │ └── NghNeighborhoodSaveRequestDto.java │ │ │ │ ├── entity │ │ │ │ │ └── NghNeighborhood.java │ │ │ │ ├── enums │ │ │ │ │ └── NghErrorMessage.java │ │ │ │ └── service │ │ │ │ │ ├── NghNeighborhoodService.java │ │ │ │ │ └── entityservice │ │ │ │ │ └── NghNeighborhoodEntityService.java │ │ │ │ └── str │ │ │ │ ├── controller │ │ │ │ └── StrStreetController.java │ │ │ │ ├── converter │ │ │ │ └── StrStreetMapper.java │ │ │ │ ├── dao │ │ │ │ └── StrStreetDao.java │ │ │ │ ├── dto │ │ │ │ ├── StrStreetDto.java │ │ │ │ └── StrStreetSaveRequestDto.java │ │ │ │ ├── entity │ │ │ │ └── StrStreet.java │ │ │ │ ├── enums │ │ │ │ └── StrErrorMessage.java │ │ │ │ └── service │ │ │ │ ├── StrStreetService.java │ │ │ │ └── entityservice │ │ │ │ └── StrStreetEntityService.java │ │ └── resources │ │ │ ├── application.properties │ │ │ └── banner.txt │ │ └── test │ │ └── java │ │ └── com │ │ └── softtech │ │ └── softtechspringboot │ │ └── SofttechSpringBootApplicationTests.java └── Case3 │ ├── README.md │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── softtech │ │ │ └── softtechspringboot │ │ │ ├── SofttechSpringBootApplication.java │ │ │ ├── cmt │ │ │ ├── controller │ │ │ │ └── CmtCommentController.java │ │ │ ├── converter │ │ │ │ └── CmtCommentMapper.java │ │ │ ├── dao │ │ │ │ └── CmtCommentDao.java │ │ │ ├── dto │ │ │ │ └── CmtCommentDto.java │ │ │ ├── entity │ │ │ │ └── CmtComment.java │ │ │ ├── enums │ │ │ │ └── CmtErrorMessage.java │ │ │ └── service │ │ │ │ ├── CmtCommentService.java │ │ │ │ └── entityservice │ │ │ │ └── CmtCommentEntityService.java │ │ │ ├── gen │ │ │ ├── dto │ │ │ │ └── RestResponse.java │ │ │ ├── entity │ │ │ │ ├── BaseAdditionalFields.java │ │ │ │ ├── BaseEntity.java │ │ │ │ └── BaseModel.java │ │ │ ├── enums │ │ │ │ ├── BaseErrorMessage.java │ │ │ │ └── GenErrorMessage.java │ │ │ ├── exception │ │ │ │ ├── GenCustomizedResponseEntityExceptionHandler.java │ │ │ │ └── GenExceptionResponse.java │ │ │ ├── exceptions │ │ │ │ ├── IllegalFieldException.java │ │ │ │ ├── ItemAlreadyExistsException.java │ │ │ │ ├── ItemNotFoundException.java │ │ │ │ └── UnmatchedFieldsException.java │ │ │ ├── service │ │ │ │ └── BaseEntityService.java │ │ │ └── util │ │ │ │ └── DateUtil.java │ │ │ ├── prd │ │ │ ├── controller │ │ │ │ └── PrdProductController.java │ │ │ ├── converter │ │ │ │ └── PrdProductMapper.java │ │ │ ├── dao │ │ │ │ └── PrdProductDao.java │ │ │ ├── dto │ │ │ │ └── PrdProductDto.java │ │ │ ├── entity │ │ │ │ └── PrdProduct.java │ │ │ ├── enums │ │ │ │ └── PrdErrorMessage.java │ │ │ └── service │ │ │ │ ├── PrdProductService.java │ │ │ │ └── entityservice │ │ │ │ └── PrdProductEntityService.java │ │ │ └── usr │ │ │ ├── controller │ │ │ └── UsrUserController.java │ │ │ ├── converter │ │ │ └── UsrUserMapper.java │ │ │ ├── dao │ │ │ └── UsrUserDao.java │ │ │ ├── dto │ │ │ ├── UsrUserDto.java │ │ │ ├── UsrUserSaveDto.java │ │ │ └── UsrUserUpdateDto.java │ │ │ ├── entity │ │ │ └── UsrUser.java │ │ │ ├── enums │ │ │ ├── UsrErrorMessage.java │ │ │ └── UsrUserType.java │ │ │ └── service │ │ │ ├── UsrUserService.java │ │ │ └── entityservice │ │ │ └── UsrUserEntityService.java │ └── resources │ │ ├── application.properties │ │ └── banner.txt │ └── test │ └── java │ └── com │ └── softtech │ └── softtechspringboot │ └── SofttechSpringBootApplicationTests.java └── README.md /Documentation/6_ Docker.md: -------------------------------------------------------------------------------- 1 | # Spring Boot Backend API 2 | 3 | # Databases using Oracle on Docker 4 | 5 | # Server and Client Side Error Handling 6 | 7 | # Packaging applications for deployment using Docker and Jib 8 | 9 | # Software Deployment Automation with Github Actions 10 | -------------------------------------------------------------------------------- /Documentation/Others.md: -------------------------------------------------------------------------------- 1 | **AJAX(Asynchronous JavaScript and XML)** 2 | AJAX is a technique for creating fast and dynamic web pages. 3 | AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. 4 | This means that it is possible to update parts of a web page, without reloading the whole page. 5 | 6 | **Servlet** 7 | A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. 8 | Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. 9 | 10 | **JSP** 11 | Jakarta Server Pages is a collection of technologies that helps software developers to create dynamically generated web pages based on HTML, XML, SOAP, or other document types. 12 | 13 | **GWT** 14 | Google Web Toolkit is an open-source set of tools that allows web developers to create and maintain JavaScript front-end applications in Java. 15 | GWT provides generic HTTP classes that you can use to build the request, and JSON and XML client classes that you can use to process the response. 16 | You can also use overlay types to convert JavaScript objects into Java objects that you can interact with in your IDE while developing. 17 | 18 | One of the benefits of using GWT is that you can leverage the tools, such as refactoring, code completion, and debugging, available in a Java IDE. 19 | 20 | **EJB** 21 | Enterprise java beans(EJB) is a server-side software component that encapsulates business logic of an application. 22 | EJB consists of 3 parts Session Bean , Message Driven Bean , Entity Bean and utility components. 23 | Entity bean is the entity structure like an object or table in the database. 24 | Session bean is the service structure that has the bussiness logic in it. 25 | Message Driven Bean allows applications to process messages asynchronously. This type of bean normally acts as a JMS message listener. 26 | 27 | **SOAP** 28 | Simple Object Access Protocol is a protocol used to interchange data between applications which are built on different programming languages. 29 | SOAP uses XML and works on HTTP methods. 30 | ![image](https://user-images.githubusercontent.com/43732258/163470253-5b706652-308a-4b82-998d-5d8ea0cca624.png) 31 | SOAP does not return human readable result, has own security, slower and requires more bandwith than REST. 32 | 33 | **Docker** 34 | Docker is an application build and deployment tool that works as a containerization platform. 35 | Container is a package of code with dependencies fitted into a deployable unit. 36 | Containers works on your os as seperate threads. 37 | Containers are stored in a repository as images, this gives you an opportunity to download and use software without installing it directly on your computer. 38 | 39 | **Kafka** 40 | Kafka is a distributed event streaming software used to build real-time streaming data pipelines and applications that adapt to the data streams. 41 | Kafka combines messaging, storage, and stream processing to allow storage and analysis of both historical and real-time data. 42 | 43 | **JIRA** 44 | Jira is a software that has a family of products designed to help teams of all types manage work. 45 | Aim is agile developement and error monitoring. 46 | 47 | **WAS** 48 | IBM WebSphere Application Server is a set of Java-based tools from IBM that allows customers to create and manage sophisticated business Web sites. 49 | 50 | **Solr** 51 | Apache Solr is a text search platform works by gathering, storing and indexing documents from different sources and making them searchable in near real-time. 52 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/GraduationProjectApplication.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class GraduationProjectApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(GraduationProjectApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.config; 2 | 3 | import io.swagger.v3.oas.models.Components; 4 | import io.swagger.v3.oas.models.OpenAPI; 5 | import io.swagger.v3.oas.models.info.Info; 6 | import io.swagger.v3.oas.models.security.SecurityRequirement; 7 | import io.swagger.v3.oas.models.security.SecurityScheme; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.util.StringUtils; 12 | 13 | @Configuration 14 | public class SwaggerConfig { 15 | 16 | @Value("${application.title}") 17 | private String APP_TITLE; 18 | 19 | @Bean 20 | public OpenAPI customOpenAPI(){ 21 | 22 | final String securitySchemeName = "bearerAuth"; 23 | final String apiTitle = String.format("%s API", StringUtils.capitalize(APP_TITLE)); 24 | 25 | return new OpenAPI() 26 | .addSecurityItem(new SecurityRequirement().addList(securitySchemeName)) 27 | .components( 28 | new Components() 29 | .addSecuritySchemes(securitySchemeName, 30 | new SecurityScheme() 31 | 32 | .name(securitySchemeName) 33 | .type(SecurityScheme.Type.HTTP) 34 | .scheme("bearer") 35 | .bearerFormat("JWT") 36 | ) 37 | ) 38 | .info(new Info().title(apiTitle).version("1")); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/dto/RestResponse.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.io.Serializable; 8 | import java.util.Date; 9 | 10 | /** 11 | * @author Bahadır Memiş 12 | * @since 1.0.0 13 | */ 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | public class RestResponse implements Serializable { 18 | 19 | private T data; 20 | private Date responseDate; 21 | private boolean isSuccess; 22 | private String messages; 23 | 24 | public RestResponse(T data, boolean isSuccess) { 25 | this.data = data; 26 | this.isSuccess = isSuccess; 27 | responseDate = new Date(); 28 | } 29 | 30 | public static RestResponse of(T t){ 31 | return new RestResponse<>(t, true); 32 | } 33 | 34 | public static RestResponse error(T t){ 35 | return new RestResponse<>(t, false); 36 | } 37 | 38 | public static RestResponse empty(){ 39 | return new RestResponse<>(null, true); 40 | } 41 | 42 | public void setMessages(String messages) { 43 | this.messages = messages; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/entity/BaseAdditionalFields.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.entity; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | import org.springframework.data.annotation.CreatedBy; 6 | import org.springframework.data.annotation.CreatedDate; 7 | import org.springframework.data.annotation.LastModifiedBy; 8 | import org.springframework.data.annotation.LastModifiedDate; 9 | 10 | import javax.persistence.Column; 11 | import javax.persistence.Embeddable; 12 | import java.util.Date; 13 | 14 | @Embeddable 15 | @Getter 16 | @Setter 17 | public class BaseAdditionalFields { 18 | 19 | @Column(name = "CREATE_DATE", updatable = false) 20 | @CreatedDate 21 | private Date createDate; 22 | 23 | @Column(name = "UPDATE_DATE") 24 | @LastModifiedDate 25 | private Date updateDate; 26 | 27 | @Column(name = "CREATED_BY") 28 | @CreatedBy 29 | private Long createdBy; 30 | 31 | @Column(name = "UPDATED_BY") 32 | @LastModifiedBy 33 | private Long updatedBy; 34 | } 35 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/entity/BaseEntity.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.entity; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import javax.persistence.Embedded; 7 | import javax.persistence.MappedSuperclass; 8 | import java.io.Serializable; 9 | 10 | @MappedSuperclass 11 | @Getter 12 | @Setter 13 | public abstract class BaseEntity implements BaseModel, Cloneable, Serializable { 14 | 15 | private static final long serialVersionUID = 1L; 16 | 17 | @Embedded 18 | private BaseAdditionalFields baseAdditionalFields; 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/entity/BaseModel.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.entity; 2 | 3 | public interface BaseModel { 4 | 5 | Long getId(); 6 | } 7 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/enums/BaseErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.enums; 2 | 3 | public interface BaseErrorMessage { 4 | 5 | String getMessage(); 6 | } 7 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/enums/GenErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.enums; 2 | 3 | public enum GenErrorMessage implements BaseErrorMessage{ 4 | 5 | ITEM_NOT_FOUND("Item not found!"), 6 | DATE_COULD_NOT_BE_CONVERTED("Date could not be converted!"), 7 | VALUE_CANNOT_BE_NEGATIVE("Value cannot be negative!"), 8 | PARAMETER_CANNOT_BE_NULL("Parameter cannot be null"), 9 | ; 10 | 11 | private String message; 12 | 13 | GenErrorMessage(String message) { 14 | this.message = message; 15 | } 16 | 17 | public String getMessage() { 18 | return message; 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return message; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/enums/GenStatusType.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.enums; 2 | 3 | public enum GenStatusType { 4 | ACTIVE, 5 | PASSIVE 6 | } 7 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/exception/GenExceptionResponse.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.exception; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | import java.util.Date; 7 | 8 | @Data 9 | @AllArgsConstructor 10 | public class GenExceptionResponse { 11 | 12 | private Date errorDate; 13 | private String message; 14 | private String detail; 15 | } 16 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/exceptions/GenBusinessException.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.exceptions; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.BaseErrorMessage; 4 | import lombok.Getter; 5 | import lombok.RequiredArgsConstructor; 6 | import lombok.Setter; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.web.bind.annotation.ResponseStatus; 9 | 10 | @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) 11 | @RequiredArgsConstructor 12 | @Getter @Setter 13 | public class GenBusinessException extends RuntimeException{ 14 | 15 | private final BaseErrorMessage baseErrorMessage; 16 | } 17 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/exceptions/IllegalFieldException.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.exceptions; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.BaseErrorMessage; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.web.bind.annotation.ResponseStatus; 6 | 7 | @ResponseStatus(HttpStatus.BAD_REQUEST) 8 | public class IllegalFieldException extends GenBusinessException{ 9 | public IllegalFieldException(BaseErrorMessage message) { 10 | super(message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/exceptions/ItemAlreadyExistsException.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.exceptions; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.BaseErrorMessage; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.web.bind.annotation.ResponseStatus; 6 | 7 | @ResponseStatus(HttpStatus.BAD_REQUEST) 8 | public class ItemAlreadyExistsException extends GenBusinessException{ 9 | public ItemAlreadyExistsException(BaseErrorMessage message) { 10 | super(message); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/exceptions/ItemNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.exceptions; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.BaseErrorMessage; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.web.bind.annotation.ResponseStatus; 6 | 7 | @ResponseStatus(HttpStatus.NOT_FOUND) 8 | public class ItemNotFoundException extends GenBusinessException{ 9 | 10 | public ItemNotFoundException(BaseErrorMessage message) { 11 | super(message); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/gen/service/BaseEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.gen.service; 2 | 3 | import com.softtech.graduationproject.app.gen.entity.BaseAdditionalFields; 4 | import com.softtech.graduationproject.app.gen.entity.BaseEntity; 5 | import com.softtech.graduationproject.app.gen.enums.GenErrorMessage; 6 | import com.softtech.graduationproject.app.gen.exceptions.ItemNotFoundException; 7 | import com.softtech.graduationproject.app.sec.service.AuthenticationService; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.context.annotation.Lazy; 11 | import org.springframework.data.jpa.repository.JpaRepository; 12 | import org.springframework.stereotype.Service; 13 | 14 | import java.util.Date; 15 | import java.util.List; 16 | import java.util.Optional; 17 | 18 | @Service 19 | @RequiredArgsConstructor 20 | public abstract class BaseEntityService> { 21 | 22 | private final D dao; 23 | 24 | private AuthenticationService authenticationService; 25 | 26 | /** To solve Circular dependency*/ 27 | 28 | @Autowired 29 | public void setAuthenticationService(@Lazy AuthenticationService authenticationService) { 30 | this.authenticationService = authenticationService; 31 | } 32 | 33 | 34 | public List findAll(){ 35 | 36 | return dao.findAll(); 37 | } 38 | 39 | public Optional findById(Long id){ 40 | 41 | return dao.findById(id); 42 | } 43 | 44 | public E save(E entity){ 45 | 46 | setAdditionalFields(entity); 47 | 48 | entity = dao.save(entity); 49 | 50 | return entity; 51 | } 52 | 53 | private void setAdditionalFields(E entity) { 54 | 55 | BaseAdditionalFields baseAdditionalFields = entity.getBaseAdditionalFields(); 56 | 57 | Long currentCustomerId = getCurrentUserId(); 58 | 59 | if (baseAdditionalFields == null){ 60 | 61 | baseAdditionalFields = new BaseAdditionalFields(); 62 | entity.setBaseAdditionalFields(baseAdditionalFields); 63 | } 64 | 65 | if (entity.getId() == null){ 66 | 67 | baseAdditionalFields.setCreateDate(new Date()); 68 | baseAdditionalFields.setCreatedBy(currentCustomerId); 69 | } 70 | 71 | baseAdditionalFields.setUpdateDate(new Date()); 72 | baseAdditionalFields.setUpdatedBy(currentCustomerId); 73 | } 74 | 75 | public void delete(E entity){ 76 | dao.delete(entity); 77 | } 78 | 79 | public E getByIdWithControl(Long id) { 80 | 81 | Optional entityOptional = findById(id); 82 | 83 | E entity; 84 | if (entityOptional.isPresent()){ 85 | entity = entityOptional.get(); 86 | } else { 87 | throw new ItemNotFoundException(GenErrorMessage.ITEM_NOT_FOUND); 88 | } 89 | 90 | return entity; 91 | } 92 | 93 | public boolean existsById(Long id){ 94 | return dao.existsById(id); 95 | } 96 | 97 | public D getDao() { 98 | return dao; 99 | } 100 | 101 | 102 | public Long getCurrentUserId() { 103 | Long currentCustomerId = authenticationService.getCurrentUserId(); 104 | return currentCustomerId; 105 | } 106 | 107 | 108 | } 109 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/converter/PrdProductMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.converter; 2 | 3 | import com.softtech.graduationproject.app.prd.dto.PrdProductDto; 4 | import com.softtech.graduationproject.app.prd.entity.PrdProduct; 5 | import com.softtech.graduationproject.app.prd.dto.PrdProductSaveRequestDto; 6 | import com.softtech.graduationproject.app.prd.dto.PrdProductUpdateRequestDto; 7 | import org.mapstruct.Mapper; 8 | import org.mapstruct.ReportingPolicy; 9 | import org.mapstruct.factory.Mappers; 10 | 11 | import java.util.List; 12 | 13 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 14 | public interface PrdProductMapper { 15 | 16 | PrdProductMapper INSTANCE = Mappers.getMapper(PrdProductMapper.class); 17 | 18 | PrdProduct convertToPrdProduct(PrdProductSaveRequestDto prdProductSaveRequestDto); 19 | 20 | PrdProduct convertToPrdProduct(PrdProductUpdateRequestDto prdProductUpdateRequestDto); 21 | 22 | List convertToPrdProductDtoList(List prdProductList); 23 | 24 | PrdProductDto convertToPrdProductDto(PrdProduct prdProduct); 25 | } 26 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/dao/PrdProductDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.dao; 2 | 3 | import com.softtech.graduationproject.app.prd.dto.PrdProductAnalysisRequestDto; 4 | import com.softtech.graduationproject.app.prd.dto.PrdVatRateDto; 5 | import com.softtech.graduationproject.app.prd.entity.PrdProduct; 6 | import org.springframework.data.jpa.repository.JpaRepository; 7 | import org.springframework.data.jpa.repository.Query; 8 | import org.springframework.data.repository.query.Param; 9 | import org.springframework.stereotype.Repository; 10 | 11 | import java.math.BigDecimal; 12 | import java.util.List; 13 | 14 | @Repository 15 | public interface PrdProductDao extends JpaRepository { 16 | 17 | 18 | List findAllByPriceBetween(BigDecimal min, BigDecimal max); 19 | 20 | 21 | List findAllByVrtVatRateId(Long vrtVatRateId); 22 | 23 | 24 | @Query( value = "SELECT "+ 25 | "new com.softtech.graduationproject.app.prd.dto.PrdProductAnalysisRequestDto( " + 26 | "vrt.productType,"+ 27 | "vrt.vatRate,"+ 28 | "min(prd.price),"+ 29 | "max(prd.price),"+ 30 | "avg(prd.price),"+ 31 | "count(prd)" + 32 | " ) " + 33 | "FROM PrdProduct prd "+ 34 | "LEFT JOIN VrtVatRate vrt "+ 35 | "ON prd.vrtVatRateId = vrt.id " + 36 | "WHERE prd.vrtVatRateId = vrt.id "+ 37 | "GROUP BY vrt.productType,vrt.vatRate" 38 | ) 39 | List getProductAnalysis(); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/dto/PrdProductAnalysisRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.dto; 2 | 3 | import com.softtech.graduationproject.app.vrt.enums.VrtProductType; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Builder; 6 | import lombok.Data; 7 | 8 | import java.math.BigDecimal; 9 | 10 | @Data 11 | @AllArgsConstructor 12 | public class PrdProductAnalysisRequestDto { 13 | 14 | private VrtProductType productType; 15 | private Integer vatRate; 16 | private BigDecimal minPrice; 17 | private BigDecimal maxPrice; 18 | private Double avgPrice; 19 | private Long productCount; 20 | } 21 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/dto/PrdProductDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.dto; 2 | 3 | import lombok.Data; 4 | 5 | 6 | import java.math.BigDecimal; 7 | 8 | @Data 9 | public class PrdProductDto { 10 | 11 | private Long id; 12 | private Long usrUserId; 13 | private Long vrtVatRateId; 14 | private String name; 15 | private BigDecimal price; 16 | } 17 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/dto/PrdProductSaveRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.dto; 2 | 3 | 4 | import lombok.*; 5 | 6 | import java.math.BigDecimal; 7 | 8 | @Getter 9 | @Setter 10 | @AllArgsConstructor 11 | public class PrdProductSaveRequestDto { 12 | 13 | private Long usrUserId; 14 | private Long vrtVatRateId; 15 | private String name; 16 | private BigDecimal vatFreePrice; 17 | } 18 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/dto/PrdProductUpdateRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.dto; 2 | 3 | import lombok.*; 4 | 5 | import java.math.BigDecimal; 6 | 7 | @Getter 8 | @Setter 9 | @AllArgsConstructor 10 | public class PrdProductUpdateRequestDto { 11 | 12 | private Long id; 13 | private Long usrUserId; 14 | private Long vrtVatRateId; 15 | private String name; 16 | private BigDecimal vatFreePrice; 17 | } 18 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/dto/PrdVatRateDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @Data 7 | @AllArgsConstructor 8 | public class PrdVatRateDto { 9 | 10 | private Long id; 11 | private Integer vatRate; 12 | } 13 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/entity/PrdProduct.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.entity; 2 | 3 | import com.softtech.graduationproject.app.gen.entity.BaseEntity; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | import javax.persistence.*; 8 | import java.math.BigDecimal; 9 | 10 | @Entity 11 | @Getter 12 | @Setter 13 | @Table(name="PRD_PRODUCT") 14 | public class PrdProduct extends BaseEntity { 15 | 16 | @Id 17 | @SequenceGenerator(name = "PrdProduct" , sequenceName = "PRD_PRODUCT_ID_SEQ") 18 | @GeneratedValue(generator = "PrdProduct") 19 | private Long id; 20 | 21 | @Column(name="ID_USR_USER",nullable = false) 22 | private Long usrUserId; 23 | 24 | @Column(name="ID_VRT_VAT_RATE",nullable = false) 25 | private Long vrtVatRateId; 26 | 27 | @Column(name="NAME",length=100, nullable = false) 28 | private String name; 29 | 30 | @Column(name="VAT_FREE_PRICE", precision = 19, scale = 2,nullable = false) 31 | private BigDecimal vatFreePrice; 32 | 33 | @Column(name="PRICE", precision = 19, scale = 2,nullable = false) 34 | private BigDecimal price; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/enums/PrdErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.enums; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.BaseErrorMessage; 4 | 5 | public enum PrdErrorMessage implements BaseErrorMessage { 6 | 7 | PRODUCT_NOT_FOUND("Product not found!"), 8 | FIELD_CANNOT_BE_NULL("Entered field cannot be null!"), 9 | 10 | VAT_FREE_PRICE_CANNOT_BE_NULL("VAT free price cannot be null!"), 11 | PRICE_CANNOT_BE_NULL("Price cannot be null!"), 12 | PRODUCT_TYPE_CANNOT_BE_NULL("Product type cannot be null!"), 13 | ID_VALUES_CANNOT_BE_NULL("Id values cannot be null!"), 14 | 15 | PRICE_MUST_BE_POSITIVE("Price must be positive!"), 16 | FIELD_MUST_BE_POSITIVE("Entered field must be positive!"), 17 | 18 | PARAMETER_MIN_CANNOT_BE_LARGER_THAN_MAX("Parameter min cannot be larger than max!") 19 | ; 20 | 21 | private String message; 22 | 23 | PrdErrorMessage(String message) { 24 | this.message = message; 25 | } 26 | 27 | public String getMessage() { 28 | return message; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return message; 34 | } 35 | } -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/service/PrdProductUtilityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.service; 2 | 3 | import com.softtech.graduationproject.app.gen.exceptions.ItemNotFoundException; 4 | import com.softtech.graduationproject.app.prd.entity.PrdProduct; 5 | import com.softtech.graduationproject.app.prd.service.entityservice.PrdProductEntityService; 6 | import com.softtech.graduationproject.app.vrt.entity.VrtVatRate; 7 | import com.softtech.graduationproject.app.vrt.enums.VrtErrorMessage; 8 | import com.softtech.graduationproject.app.vrt.service.entityservice.VrtVatRateEntityService; 9 | import lombok.RequiredArgsConstructor; 10 | import org.springframework.stereotype.Service; 11 | import org.springframework.transaction.annotation.Transactional; 12 | 13 | import java.math.BigDecimal; 14 | 15 | @Service 16 | @Transactional 17 | @RequiredArgsConstructor 18 | public class PrdProductUtilityService { 19 | 20 | private final PrdProductValidationService prdProductValidationService; 21 | 22 | private final VrtVatRateEntityService vrtVatRateEntityService; 23 | 24 | public BigDecimal calculatePriceWithControl(PrdProduct prdProduct){ 25 | 26 | Long vrtVatRateId = prdProduct.getVrtVatRateId(); 27 | 28 | VrtVatRate vrtVatRate = vrtVatRateEntityService.findVrtVatRateById(vrtVatRateId) 29 | .orElseThrow(()-> new ItemNotFoundException(VrtErrorMessage.VAT_RATE_NOT_FOUND)); 30 | 31 | Integer vatRateInt = vrtVatRate.getVatRate(); 32 | 33 | Double vatRate = Double.valueOf(vatRateInt); 34 | 35 | BigDecimal vatFreePrice = prdProduct.getVatFreePrice(); 36 | 37 | prdProductValidationService.controlIsPriceNull(vatFreePrice); 38 | 39 | BigDecimal price = vatFreePrice.add(vatFreePrice.multiply(BigDecimal.valueOf(vatRate/100))); 40 | 41 | prdProductValidationService.controlIsPricePositive(price); 42 | 43 | return price; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/prd/service/entityservice/PrdProductEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.prd.service.entityservice; 2 | 3 | import com.softtech.graduationproject.app.gen.service.BaseEntityService; 4 | import com.softtech.graduationproject.app.prd.dao.PrdProductDao; 5 | import com.softtech.graduationproject.app.prd.dto.PrdProductAnalysisRequestDto; 6 | import com.softtech.graduationproject.app.prd.entity.PrdProduct; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import java.math.BigDecimal; 11 | import java.util.List; 12 | import java.util.Optional; 13 | 14 | @Service 15 | @Transactional 16 | public class PrdProductEntityService extends BaseEntityService { 17 | 18 | 19 | public PrdProductEntityService(PrdProductDao prdProductDao){ 20 | 21 | super(prdProductDao); 22 | } 23 | 24 | 25 | public List findProductsByPriceInterval(BigDecimal min, BigDecimal max) { 26 | 27 | List prdProductList = getDao().findAllByPriceBetween(min,max); 28 | 29 | return prdProductList; 30 | } 31 | 32 | 33 | public List findProductsByVatRateId(Long vrtVatRateId) { 34 | 35 | List prdProductList = getDao().findAllByVrtVatRateId(vrtVatRateId); 36 | 37 | return prdProductList; 38 | } 39 | 40 | 41 | public List getProductAnalysis() { 42 | 43 | List prdProductAnalysisRequestDtoList = getDao().getProductAnalysis(); 44 | 45 | return prdProductAnalysisRequestDtoList; 46 | } 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/sec/controller/AuthenticationController.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.sec.controller; 2 | 3 | 4 | import com.softtech.graduationproject.app.gen.dto.RestResponse; 5 | import com.softtech.graduationproject.app.sec.dto.SecLoginRequestDto; 6 | import com.softtech.graduationproject.app.sec.service.AuthenticationService; 7 | import com.softtech.graduationproject.app.usr.dto.UsrUserDto; 8 | import com.softtech.graduationproject.app.usr.dto.UsrUserSaveRequestDto; 9 | import io.swagger.v3.oas.annotations.Operation; 10 | import lombok.RequiredArgsConstructor; 11 | import org.springframework.http.ResponseEntity; 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.RestController; 16 | 17 | @RestController 18 | @RequestMapping("/auth") 19 | @RequiredArgsConstructor 20 | public class AuthenticationController { 21 | 22 | 23 | private final AuthenticationService authenticationService; 24 | 25 | 26 | @Operation(tags = "Authentication Controller") 27 | @PostMapping("/login") 28 | public ResponseEntity> login(@RequestBody SecLoginRequestDto secLoginRequestDto){ 29 | 30 | String token = authenticationService.login(secLoginRequestDto); 31 | 32 | return ResponseEntity.ok(RestResponse.of(token)); 33 | } 34 | 35 | 36 | @Operation(tags = "Authentication Controller") 37 | @PostMapping("/register") 38 | public ResponseEntity> register(@RequestBody UsrUserSaveRequestDto usrUserSaveRequestDto){ 39 | 40 | UsrUserDto usrUserDto =authenticationService.register(usrUserSaveRequestDto); 41 | 42 | return ResponseEntity.ok(RestResponse.of(usrUserDto)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/sec/dto/SecLoginRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.sec.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class SecLoginRequestDto { 7 | 8 | private String username; 9 | private String password; 10 | } 11 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/sec/enums/EnumJwtConstant.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.sec.enums; 2 | 3 | public enum EnumJwtConstant { 4 | 5 | BEARER("Bearer ") 6 | ; 7 | 8 | private String constant; 9 | EnumJwtConstant(String constant) { 10 | this.constant = constant; 11 | } 12 | 13 | public String getConstant() { 14 | return constant; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return constant; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/sec/security/JwtAuthenticationEntryPoint.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.sec.security; 2 | 3 | import org.springframework.security.core.AuthenticationException; 4 | import org.springframework.security.web.AuthenticationEntryPoint; 5 | import org.springframework.stereotype.Component; 6 | 7 | import javax.servlet.ServletException; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import java.io.IOException; 11 | 12 | @Component 13 | public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { 14 | 15 | @Override 16 | public void commence(HttpServletRequest request, HttpServletResponse response, 17 | AuthenticationException authException)throws IOException, ServletException { 18 | response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/sec/security/JwtAuthenticationFilter.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.sec.security; 2 | 3 | import com.softtech.graduationproject.app.sec.enums.EnumJwtConstant; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 6 | import org.springframework.security.core.context.SecurityContextHolder; 7 | import org.springframework.security.core.userdetails.UserDetails; 8 | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; 9 | import org.springframework.util.StringUtils; 10 | import org.springframework.web.filter.OncePerRequestFilter; 11 | 12 | import javax.servlet.FilterChain; 13 | import javax.servlet.ServletException; 14 | import javax.servlet.http.HttpServletRequest; 15 | import javax.servlet.http.HttpServletResponse; 16 | import java.io.IOException; 17 | 18 | public class JwtAuthenticationFilter extends OncePerRequestFilter { 19 | 20 | @Autowired 21 | private JwtTokenGenerator jwtTokenGenerator; 22 | 23 | @Autowired 24 | private JwtUserDetailsService jwtUserDetailsService; 25 | 26 | @Override 27 | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, 28 | FilterChain filterChain) throws ServletException, IOException { 29 | 30 | String token = getToken(request); 31 | 32 | if (StringUtils.hasText(token)){ 33 | 34 | boolean isValid = jwtTokenGenerator.validateToken(token); 35 | 36 | if (isValid){ 37 | 38 | Long userId = jwtTokenGenerator.findUserIdByToken(token); 39 | 40 | UserDetails userDetails = jwtUserDetailsService.loadUserByUserId(userId); 41 | 42 | if (userDetails != null){ 43 | 44 | UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); 45 | authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); 46 | 47 | SecurityContextHolder.getContext().setAuthentication(authenticationToken); 48 | } 49 | } 50 | } 51 | 52 | filterChain.doFilter(request, response); 53 | } 54 | 55 | private String getToken(HttpServletRequest request) { 56 | String fullToken = request.getHeader("Authorization"); 57 | 58 | String token = null; 59 | if (StringUtils.hasText(fullToken)){ 60 | String bearer = EnumJwtConstant.BEARER.getConstant(); 61 | 62 | if (fullToken.startsWith(bearer)){ 63 | token = fullToken.substring(bearer.length()); 64 | } 65 | } 66 | return token; 67 | } 68 | } -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/sec/security/JwtTokenGenerator.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.sec.security; 2 | 3 | import io.jsonwebtoken.Claims; 4 | import io.jsonwebtoken.Jws; 5 | import io.jsonwebtoken.Jwts; 6 | import io.jsonwebtoken.SignatureAlgorithm; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.security.core.Authentication; 9 | import org.springframework.stereotype.Component; 10 | 11 | import java.util.Date; 12 | 13 | @Component 14 | public class JwtTokenGenerator { 15 | 16 | @Value("${softtechspringboot.jwt.security.app.key}") 17 | private String APP_KEY; 18 | 19 | @Value("${softtechspringboot.jwt.security.expire.time}") 20 | private Long EXPIRE_TIME; 21 | 22 | public String generateJwtToken(Authentication authentication){ 23 | 24 | JwtUserDetails jwtUserDetails = (JwtUserDetails) authentication.getPrincipal(); 25 | Date expireDate = new Date(new Date().getTime() + EXPIRE_TIME); 26 | 27 | String token = Jwts.builder() 28 | .setSubject(Long.toString(jwtUserDetails.getId())) 29 | .setIssuedAt(new Date()) 30 | .setExpiration(expireDate) 31 | .signWith(SignatureAlgorithm.HS512, APP_KEY) 32 | .compact(); 33 | 34 | return token; 35 | } 36 | 37 | public Long findUserIdByToken(String token){ 38 | 39 | Jws claimsJws = parseToken(token); 40 | 41 | String userIdStr = claimsJws 42 | .getBody() 43 | .getSubject(); 44 | 45 | return Long.parseLong(userIdStr); 46 | } 47 | 48 | private Jws parseToken(String token) { 49 | Jws claimsJws = Jwts.parser() 50 | .setSigningKey(APP_KEY) 51 | .parseClaimsJws(token); 52 | return claimsJws; 53 | } 54 | 55 | public boolean validateToken(String token){ 56 | 57 | boolean isValid; 58 | 59 | try { 60 | Jws claimsJws = parseToken(token); 61 | 62 | isValid = !isTokenExpired(claimsJws); 63 | } catch (Exception e){ 64 | isValid = false; 65 | } 66 | 67 | return isValid; 68 | } 69 | 70 | private boolean isTokenExpired(Jws claimsJws) { 71 | 72 | Date expirationDate = claimsJws.getBody().getExpiration(); 73 | 74 | return expirationDate.before(new Date()); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/sec/security/JwtUserDetails.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.sec.security; 2 | 3 | import com.softtech.graduationproject.app.usr.entity.UsrUser; 4 | import org.springframework.security.core.GrantedAuthority; 5 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 6 | import org.springframework.security.core.userdetails.UserDetails; 7 | 8 | import java.util.ArrayList; 9 | import java.util.Collection; 10 | import java.util.List; 11 | 12 | public class JwtUserDetails implements UserDetails { 13 | 14 | private Long id; 15 | private String username; 16 | private String password; 17 | private Collection authorities; 18 | 19 | private JwtUserDetails(Long id, String username, String password, Collection authorities) { 20 | this.id = id; 21 | this.username = username; 22 | this.password = password; 23 | this.authorities = authorities; 24 | } 25 | 26 | public static JwtUserDetails create(UsrUser usrUser){ 27 | 28 | Long id = usrUser.getId(); 29 | String username = usrUser.getUsername(); 30 | String password = usrUser.getPassword(); 31 | 32 | List grantedAuthorityList = new ArrayList<>(); 33 | grantedAuthorityList.add(new SimpleGrantedAuthority("user")); 34 | 35 | return new JwtUserDetails(id, username, password, grantedAuthorityList); 36 | } 37 | 38 | @Override 39 | public Collection getAuthorities() { 40 | return authorities; 41 | } 42 | 43 | @Override 44 | public String getPassword() { 45 | return password; 46 | } 47 | 48 | @Override 49 | public String getUsername() { 50 | return username; 51 | } 52 | 53 | @Override 54 | public boolean isAccountNonExpired() { 55 | return true; 56 | } 57 | 58 | @Override 59 | public boolean isAccountNonLocked() { 60 | return true; 61 | } 62 | 63 | @Override 64 | public boolean isCredentialsNonExpired() { 65 | return true; 66 | } 67 | 68 | @Override 69 | public boolean isEnabled() { 70 | return true; 71 | } 72 | 73 | public Long getId() { 74 | return id; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/sec/security/JwtUserDetailsService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.sec.security; 2 | 3 | import com.softtech.graduationproject.app.gen.exceptions.ItemNotFoundException; 4 | import com.softtech.graduationproject.app.usr.entity.UsrUser; 5 | import com.softtech.graduationproject.app.usr.enums.UsrErrorMessage; 6 | import com.softtech.graduationproject.app.usr.service.entityservice.UsrUserEntityService; 7 | import lombok.RequiredArgsConstructor; 8 | import org.springframework.security.core.userdetails.UserDetails; 9 | import org.springframework.security.core.userdetails.UserDetailsService; 10 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 11 | import org.springframework.stereotype.Service; 12 | 13 | @Service 14 | @RequiredArgsConstructor 15 | public class JwtUserDetailsService implements UserDetailsService { 16 | 17 | private final UsrUserEntityService usrUserEntityService; 18 | 19 | @Override 20 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 21 | 22 | UsrUser usrUser = usrUserEntityService.findUsersByUsername(username) 23 | .orElseThrow(()-> new ItemNotFoundException(UsrErrorMessage.USER_NOT_FOUND)); 24 | 25 | return JwtUserDetails.create(usrUser); 26 | } 27 | 28 | public UserDetails loadUserByUserId(Long id) { 29 | 30 | UsrUser usrUser = usrUserEntityService.getByIdWithControl(id); 31 | 32 | return JwtUserDetails.create(usrUser); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/sec/service/AuthenticationService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.sec.service; 2 | 3 | 4 | import com.softtech.graduationproject.app.sec.dto.SecLoginRequestDto; 5 | import com.softtech.graduationproject.app.sec.enums.EnumJwtConstant; 6 | import com.softtech.graduationproject.app.sec.security.JwtTokenGenerator; 7 | import com.softtech.graduationproject.app.sec.security.JwtUserDetails; 8 | import com.softtech.graduationproject.app.usr.dto.UsrUserDto; 9 | import com.softtech.graduationproject.app.usr.dto.UsrUserSaveRequestDto; 10 | import com.softtech.graduationproject.app.usr.entity.UsrUser; 11 | import com.softtech.graduationproject.app.usr.service.UsrUserService; 12 | import com.softtech.graduationproject.app.usr.service.entityservice.UsrUserEntityService; 13 | import lombok.RequiredArgsConstructor; 14 | import org.springframework.security.authentication.AuthenticationManager; 15 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 16 | import org.springframework.security.core.Authentication; 17 | import org.springframework.security.core.context.SecurityContextHolder; 18 | import org.springframework.stereotype.Service; 19 | 20 | 21 | @Service 22 | @RequiredArgsConstructor 23 | public class AuthenticationService { 24 | 25 | private final UsrUserService usrUserService; 26 | private final UsrUserEntityService usrUserEntityService; 27 | private final AuthenticationManager authenticationManager; 28 | private final JwtTokenGenerator jwtTokenGenerator; 29 | 30 | public UsrUserDto register(UsrUserSaveRequestDto usrUserSaveRequestDto) { 31 | 32 | UsrUserDto usrUserDto = usrUserService.saveUser(usrUserSaveRequestDto); 33 | 34 | return usrUserDto; 35 | } 36 | 37 | public String login(SecLoginRequestDto secLoginRequestDto) { 38 | 39 | UsernamePasswordAuthenticationToken authenticationToken = 40 | new UsernamePasswordAuthenticationToken( 41 | secLoginRequestDto.getUsername(), 42 | secLoginRequestDto.getPassword() 43 | ); 44 | 45 | Authentication authentication = authenticationManager.authenticate(authenticationToken); 46 | SecurityContextHolder.getContext().setAuthentication(authentication); 47 | 48 | String token = jwtTokenGenerator.generateJwtToken(authentication); 49 | 50 | String bearer = EnumJwtConstant.BEARER.getConstant(); 51 | 52 | return bearer + token; 53 | } 54 | 55 | public UsrUser getCurrentUser() { 56 | 57 | JwtUserDetails jwtUserDetails = getCurrentJwtUserDetails(); 58 | 59 | UsrUser usrUser = null; 60 | if (jwtUserDetails != null){ 61 | usrUser = usrUserEntityService.getByIdWithControl(jwtUserDetails.getId()); 62 | } 63 | 64 | return usrUser; 65 | } 66 | 67 | public Long getCurrentUserId(){ 68 | 69 | JwtUserDetails jwtUserDetails = getCurrentJwtUserDetails(); 70 | 71 | Long jwtUserDetailsId = null; 72 | if (jwtUserDetails != null){ 73 | jwtUserDetailsId = jwtUserDetails.getId(); 74 | } 75 | 76 | return jwtUserDetailsId; 77 | } 78 | 79 | private JwtUserDetails getCurrentJwtUserDetails() { 80 | 81 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 82 | 83 | JwtUserDetails jwtUserDetails = null; 84 | if (authentication != null && authentication.getPrincipal() instanceof JwtUserDetails){ 85 | jwtUserDetails = (JwtUserDetails) authentication.getPrincipal(); 86 | } 87 | return jwtUserDetails; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/usr/converter/UsrUserMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.usr.converter; 2 | 3 | import com.softtech.graduationproject.app.usr.dto.UsrUserDto; 4 | import com.softtech.graduationproject.app.usr.dto.UsrUserFindByIdRequestDto; 5 | import com.softtech.graduationproject.app.usr.dto.UsrUserSaveRequestDto; 6 | import com.softtech.graduationproject.app.usr.dto.UsrUserUpdateRequestDto; 7 | import com.softtech.graduationproject.app.usr.entity.UsrUser; 8 | import org.mapstruct.Mapper; 9 | import org.mapstruct.ReportingPolicy; 10 | import org.mapstruct.factory.Mappers; 11 | 12 | import java.util.List; 13 | 14 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 15 | public interface UsrUserMapper { 16 | 17 | UsrUserMapper INSTANCE = Mappers.getMapper(UsrUserMapper.class); 18 | 19 | UsrUser convertToUsrUser(UsrUserSaveRequestDto usrUserSaveRequestDto); 20 | 21 | UsrUser convertToUsrUser(UsrUserUpdateRequestDto usrUserUpdateRequestDto); 22 | 23 | UsrUserFindByIdRequestDto convertToUsrUserFindByIdRequestDto(UsrUser usrUser); 24 | 25 | List convertToUsrUserDtoList(List usrUserList); 26 | 27 | UsrUserDto convertToUsrUserDto(UsrUser cusCustomer); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/usr/dao/UsrUserDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.usr.dao; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.GenStatusType; 4 | import com.softtech.graduationproject.app.usr.entity.UsrUser; 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.util.List; 9 | import java.util.Optional; 10 | 11 | @Repository 12 | public interface UsrUserDao extends JpaRepository { 13 | 14 | List findAllByStatusType(GenStatusType statusType); 15 | 16 | Optional findByUsername(String username); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/usr/dto/UsrUserDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.usr.dto; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.GenStatusType; 4 | import lombok.Data; 5 | 6 | @Data 7 | public class UsrUserDto { 8 | 9 | private Long id; 10 | private String name; 11 | private String surname; 12 | private String username; 13 | } 14 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/usr/dto/UsrUserFindByIdRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.usr.dto; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.GenStatusType; 4 | import lombok.Data; 5 | 6 | @Data 7 | public class UsrUserFindByIdRequestDto { 8 | 9 | private Long id; 10 | private String name; 11 | private String surname; 12 | private String username; 13 | private GenStatusType statusType; 14 | } 15 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/usr/dto/UsrUserSaveRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.usr.dto; 2 | 3 | import lombok.*; 4 | 5 | @Getter 6 | @Setter 7 | @AllArgsConstructor 8 | public class UsrUserSaveRequestDto { 9 | 10 | private String name; 11 | private String surname; 12 | private String username; 13 | private String password; 14 | } 15 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/usr/dto/UsrUserUpdateRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.usr.dto; 2 | 3 | import lombok.*; 4 | 5 | @Getter 6 | @Setter 7 | @AllArgsConstructor 8 | public class UsrUserUpdateRequestDto { 9 | 10 | private Long id; 11 | private String name; 12 | private String surname; 13 | private String username; 14 | private String password; 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/usr/entity/UsrUser.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.usr.entity; 2 | 3 | import com.softtech.graduationproject.app.gen.entity.BaseEntity; 4 | import com.softtech.graduationproject.app.gen.enums.GenStatusType; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import javax.persistence.*; 9 | 10 | @Entity 11 | @Getter 12 | @Setter 13 | @Table(name="USR_USER") 14 | public class UsrUser extends BaseEntity { 15 | @Id 16 | @SequenceGenerator(name = "UsrUser" , sequenceName = "USR_USER_ID_SEQ") 17 | @GeneratedValue(generator = "UsrUser") 18 | private Long id; 19 | 20 | @Column(name="NAME",length=100, nullable = false) 21 | private String name; 22 | 23 | @Column(name="SURNAME",length=100, nullable = false) 24 | private String surname; 25 | 26 | @Column(name="USERNAME",length=100,nullable = false,unique = true) 27 | private String username; 28 | 29 | @Column(name = "PASSWORD", nullable = false) 30 | private String password; 31 | 32 | @Enumerated(EnumType.STRING) 33 | @Column(name = "STATUS_TYPE", length = 30, nullable = false) 34 | private GenStatusType statusType; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/usr/enums/UsrErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.usr.enums; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.BaseErrorMessage; 4 | 5 | public enum UsrErrorMessage implements BaseErrorMessage { 6 | 7 | USER_NOT_FOUND("User not found!"), 8 | USERNAME_ALREADY_EXIST("Username already exists!"), 9 | FIELD_CANNOT_BE_NULL("Field cannot be null!") 10 | ; 11 | 12 | private String message; 13 | 14 | UsrErrorMessage(String message) { 15 | this.message = message; 16 | } 17 | 18 | public String getMessage() { 19 | return message; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return message; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/usr/service/UsrUserValidationService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.usr.service; 2 | 3 | import com.softtech.graduationproject.app.gen.entity.BaseEntity; 4 | import com.softtech.graduationproject.app.gen.exceptions.IllegalFieldException; 5 | import com.softtech.graduationproject.app.gen.exceptions.ItemAlreadyExistsException; 6 | import com.softtech.graduationproject.app.gen.exceptions.ItemNotFoundException; 7 | import com.softtech.graduationproject.app.usr.dto.UsrUserUpdateRequestDto; 8 | import com.softtech.graduationproject.app.usr.entity.UsrUser; 9 | import com.softtech.graduationproject.app.usr.enums.UsrErrorMessage; 10 | import com.softtech.graduationproject.app.usr.service.entityservice.UsrUserEntityService; 11 | import lombok.RequiredArgsConstructor; 12 | import org.springframework.stereotype.Service; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | import java.util.Optional; 16 | 17 | @Service 18 | @Transactional 19 | @RequiredArgsConstructor 20 | public class UsrUserValidationService { 21 | 22 | 23 | private final UsrUserEntityService usrUserEntityService; 24 | 25 | 26 | public void controlIsUserExist(Long id) { 27 | 28 | boolean isExist = usrUserEntityService.existsById(id); 29 | 30 | if (!isExist){ 31 | 32 | throw new ItemNotFoundException(UsrErrorMessage.USER_NOT_FOUND); 33 | } 34 | } 35 | 36 | 37 | public void controlAreFieldsNonNull(UsrUser usrUser){ 38 | 39 | boolean hasNullField = 40 | usrUser.getName().isBlank() || 41 | usrUser.getSurname().isBlank() || 42 | usrUser.getUsername().isBlank() || 43 | usrUser.getPassword().isBlank(); 44 | 45 | if(hasNullField){ 46 | 47 | throw new IllegalFieldException(UsrErrorMessage.FIELD_CANNOT_BE_NULL); 48 | } 49 | } 50 | 51 | 52 | /*It does not throw exception when not present because that is the best case behaviour.*/ 53 | public void controlIsUsernameUnique(UsrUser usrUser){ 54 | 55 | Optional usrUserOptional = usrUserEntityService.findUsersByUsername(usrUser.getUsername()); 56 | 57 | UsrUser usrUserReturned; 58 | if(usrUserOptional.isPresent()){ 59 | 60 | usrUserReturned = usrUserOptional.get(); 61 | 62 | boolean didMatchedItself = didMatchedItself(usrUserReturned, usrUser); 63 | 64 | if(!didMatchedItself){ 65 | throw new ItemAlreadyExistsException(UsrErrorMessage.USERNAME_ALREADY_EXIST); 66 | } 67 | 68 | } 69 | 70 | } 71 | 72 | 73 | private Boolean didMatchedItself(UsrUser usrUserReturned, BaseEntity usrUser){ 74 | 75 | return usrUserReturned.getId().equals(usrUser.getId()); 76 | } 77 | 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/usr/service/entityservice/UsrUserEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.usr.service.entityservice; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.GenStatusType; 4 | import com.softtech.graduationproject.app.gen.service.BaseEntityService; 5 | import com.softtech.graduationproject.app.usr.dao.UsrUserDao; 6 | import com.softtech.graduationproject.app.usr.entity.UsrUser; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import java.util.List; 11 | import java.util.Optional; 12 | 13 | 14 | @Service 15 | @Transactional 16 | public class UsrUserEntityService extends BaseEntityService { 17 | 18 | public UsrUserEntityService(UsrUserDao usrUserDao) { 19 | super(usrUserDao); 20 | } 21 | 22 | public List findAllActiveUsers(){ 23 | 24 | return getDao().findAllByStatusType(GenStatusType.ACTIVE); 25 | } 26 | 27 | public Optional findUsersByUsername(String username){ 28 | 29 | return getDao().findByUsername(username); 30 | 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/converter/VrtVatRateMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.converter; 2 | 3 | import com.softtech.graduationproject.app.vrt.dto.VrtVatRateDto; 4 | import com.softtech.graduationproject.app.vrt.dto.VrtVatRateSaveRequestDto; 5 | import com.softtech.graduationproject.app.vrt.dto.VrtVatRateUpdateRequestDto; 6 | import com.softtech.graduationproject.app.vrt.entity.VrtVatRate; 7 | import org.mapstruct.Mapper; 8 | import org.mapstruct.ReportingPolicy; 9 | import org.mapstruct.factory.Mappers; 10 | 11 | import java.util.List; 12 | 13 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 14 | public interface VrtVatRateMapper { 15 | 16 | VrtVatRateMapper INSTANCE = Mappers.getMapper(VrtVatRateMapper.class); 17 | 18 | VrtVatRateDto convertToVrtVatRateDto(VrtVatRate vrtVatRate); 19 | 20 | List convertToVrtVatRateDtoList(List vrtVatRateList); 21 | 22 | VrtVatRate convertToVrtVatRate(VrtVatRateSaveRequestDto vrtVatRateSaveRequestDto); 23 | VrtVatRate convertToVrtVatRate(VrtVatRateUpdateRequestDto vrtVatRateUpdateRequestDto); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/dao/VrtVatRateDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.dao; 2 | 3 | import com.softtech.graduationproject.app.usr.entity.UsrUser; 4 | import com.softtech.graduationproject.app.vrt.entity.VrtVatRate; 5 | import com.softtech.graduationproject.app.vrt.enums.VrtProductType; 6 | import org.springframework.data.jpa.repository.JpaRepository; 7 | import org.springframework.stereotype.Repository; 8 | 9 | import java.util.Optional; 10 | 11 | @Repository 12 | public interface VrtVatRateDao extends JpaRepository { 13 | 14 | Optional findByProductType(VrtProductType vrtProductType); 15 | 16 | Optional findVrtVatRateById(Long id); 17 | } 18 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/dto/VrtVatRateDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.dto; 2 | 3 | import com.softtech.graduationproject.app.vrt.enums.VrtProductType; 4 | import lombok.Data; 5 | 6 | 7 | @Data 8 | public class VrtVatRateDto { 9 | 10 | private Long id; 11 | private VrtProductType productType; 12 | private Integer vatRate; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/dto/VrtVatRateSaveRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.dto; 2 | 3 | import com.softtech.graduationproject.app.vrt.enums.VrtProductType; 4 | import lombok.*; 5 | 6 | @Getter 7 | @Setter 8 | @AllArgsConstructor 9 | public class VrtVatRateSaveRequestDto { 10 | 11 | private VrtProductType productType; 12 | private Integer vatRate; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/dto/VrtVatRateUpdateRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.dto; 2 | 3 | import com.softtech.graduationproject.app.vrt.enums.VrtProductType; 4 | import lombok.*; 5 | 6 | @Getter 7 | @Setter 8 | @AllArgsConstructor 9 | public class VrtVatRateUpdateRequestDto { 10 | 11 | private Long id; 12 | private VrtProductType productType; 13 | private Integer vatRate; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/entity/VrtVatRate.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.entity; 2 | 3 | import com.softtech.graduationproject.app.gen.entity.BaseEntity; 4 | import com.softtech.graduationproject.app.vrt.enums.VrtProductType; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | 9 | import javax.persistence.*; 10 | 11 | @Entity 12 | @Getter 13 | @Setter 14 | @Table(name="VRT_VAT_RATE") 15 | public class VrtVatRate extends BaseEntity { 16 | 17 | @Id 18 | @SequenceGenerator(name = "VrtVatRate" , sequenceName = "VRT_VAT_RATE_ID_SEQ") 19 | @GeneratedValue(generator = "VrtVatRate") 20 | private Long id; 21 | 22 | @Enumerated(EnumType.STRING) 23 | @Column(name="PRODUCT_TYPE",length=30, nullable = false, unique = true) 24 | private VrtProductType productType; 25 | 26 | @Column(name="VAT_RATE",nullable = false) 27 | private Integer vatRate; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/enums/VrtErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.enums; 2 | 3 | import com.softtech.graduationproject.app.gen.enums.BaseErrorMessage; 4 | 5 | public enum VrtErrorMessage implements BaseErrorMessage { 6 | 7 | VAT_RATE_NOT_FOUND("VAT rate not found!"), 8 | VAT_RATE_CANNOT_BE_NEGATIVE("VAT rate cannot be negative!"), 9 | VAT_RATE_CANNOT_BE_NULL("VAT rate cannot be null!"), 10 | VAT_RATE_ALREADY_EXIST("VAT rate already exist!"), 11 | FIELDS_CANNOT_BE_NULL("Fields cannot be null!"), 12 | ; 13 | 14 | private String message; 15 | 16 | VrtErrorMessage(String message) { 17 | this.message = message; 18 | } 19 | 20 | public String getMessage() { 21 | return message; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return message; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/enums/VrtProductType.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.enums; 2 | 3 | public enum VrtProductType { 4 | FOOD, 5 | STATIONARY, 6 | CLOTHING, 7 | TECHNOLOGY, 8 | CLEANING, 9 | OTHER 10 | } 11 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/service/VrtVatRateService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.service; 2 | 3 | import com.softtech.graduationproject.app.prd.service.PrdProductService; 4 | import com.softtech.graduationproject.app.vrt.converter.VrtVatRateMapper; 5 | import com.softtech.graduationproject.app.vrt.dto.VrtVatRateDto; 6 | import com.softtech.graduationproject.app.vrt.dto.VrtVatRateSaveRequestDto; 7 | import com.softtech.graduationproject.app.vrt.dto.VrtVatRateUpdateRequestDto; 8 | import com.softtech.graduationproject.app.vrt.entity.VrtVatRate; 9 | import com.softtech.graduationproject.app.vrt.service.entityservice.VrtVatRateEntityService; 10 | import lombok.RequiredArgsConstructor; 11 | import org.springframework.stereotype.Service; 12 | import org.springframework.transaction.annotation.Transactional; 13 | 14 | import java.util.List; 15 | 16 | @Service 17 | @RequiredArgsConstructor 18 | @Transactional 19 | public class VrtVatRateService { 20 | 21 | private final VrtVatRateEntityService vrtVatRateEntityService; 22 | private final PrdProductService prdProductService; 23 | 24 | private final VrtVatRateValidationService vrtVatRateValidationService; 25 | 26 | public List findAllVatRates() { 27 | 28 | List vrtVatRateList = vrtVatRateEntityService.findAll(); 29 | 30 | List VrtVatRateDtoList = VrtVatRateMapper.INSTANCE.convertToVrtVatRateDtoList(vrtVatRateList); 31 | 32 | return VrtVatRateDtoList; 33 | } 34 | 35 | 36 | public VrtVatRateDto saveVatRate(VrtVatRateSaveRequestDto vrtVatRateSaveRequestDto) { 37 | 38 | VrtVatRate vrtVatRate = VrtVatRateMapper.INSTANCE.convertToVrtVatRate(vrtVatRateSaveRequestDto); 39 | 40 | vrtVatRateValidationService.controlAreFieldsNonNull(vrtVatRate); 41 | vrtVatRateValidationService.controlIsVatRateNegative(vrtVatRate); 42 | vrtVatRateValidationService.controlIsProductTypeUnique(vrtVatRate); 43 | 44 | vrtVatRate = vrtVatRateEntityService.save(vrtVatRate); 45 | 46 | VrtVatRateDto vrtVatRateDto = VrtVatRateMapper.INSTANCE.convertToVrtVatRateDto(vrtVatRate); 47 | 48 | return vrtVatRateDto; 49 | } 50 | 51 | 52 | public VrtVatRateDto updateVatRate(VrtVatRateUpdateRequestDto vrtVatRateUpdateRequestDto) { 53 | 54 | Long id = vrtVatRateUpdateRequestDto.getId(); 55 | 56 | vrtVatRateValidationService.controlIsVrtVatRateExist(id); 57 | 58 | VrtVatRate vrtVatRate = VrtVatRateMapper.INSTANCE.convertToVrtVatRate(vrtVatRateUpdateRequestDto); 59 | 60 | vrtVatRateValidationService.controlAreFieldsNonNull(vrtVatRate); 61 | vrtVatRateValidationService.controlIsVatRateNegative(vrtVatRate); 62 | vrtVatRateValidationService.controlIsProductTypeUnique(vrtVatRate); 63 | 64 | vrtVatRate = vrtVatRateEntityService.save(vrtVatRate); 65 | 66 | batchProductUpdate(id); 67 | 68 | VrtVatRateDto vrtVatRateDto = VrtVatRateMapper.INSTANCE.convertToVrtVatRateDto(vrtVatRate); 69 | 70 | return vrtVatRateDto; 71 | } 72 | 73 | 74 | private void batchProductUpdate(Long vrtVatRateId){ 75 | 76 | vrtVatRateValidationService.controlIsVrtVatRateExist(vrtVatRateId); 77 | 78 | prdProductService.batchProductUpdate(vrtVatRateId); 79 | 80 | } 81 | 82 | 83 | public void deleteVatRate(Long id) { 84 | 85 | VrtVatRate vrtVatRate = vrtVatRateEntityService.getByIdWithControl(id); 86 | 87 | vrtVatRateEntityService.delete(vrtVatRate); 88 | 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/service/VrtVatRateValidationService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.service; 2 | 3 | import com.softtech.graduationproject.app.gen.entity.BaseEntity; 4 | import com.softtech.graduationproject.app.gen.exceptions.IllegalFieldException; 5 | import com.softtech.graduationproject.app.gen.exceptions.ItemAlreadyExistsException; 6 | import com.softtech.graduationproject.app.gen.exceptions.ItemNotFoundException; 7 | import com.softtech.graduationproject.app.vrt.entity.VrtVatRate; 8 | import com.softtech.graduationproject.app.vrt.enums.VrtErrorMessage; 9 | import com.softtech.graduationproject.app.vrt.enums.VrtProductType; 10 | import com.softtech.graduationproject.app.vrt.service.entityservice.VrtVatRateEntityService; 11 | import lombok.RequiredArgsConstructor; 12 | import org.springframework.stereotype.Service; 13 | import org.springframework.transaction.annotation.Transactional; 14 | 15 | import java.util.Optional; 16 | 17 | @Service 18 | @Transactional 19 | @RequiredArgsConstructor 20 | public class VrtVatRateValidationService { 21 | 22 | private final VrtVatRateEntityService vrtVatRateEntityService; 23 | 24 | public void controlIsVrtVatRateExist(Long id){ 25 | 26 | boolean isExist = vrtVatRateEntityService.existsById(id); 27 | if (!isExist){ 28 | throw new ItemNotFoundException(VrtErrorMessage.VAT_RATE_NOT_FOUND); 29 | } 30 | 31 | } 32 | 33 | public void controlAreFieldsNonNull(VrtVatRate vrtVatRate){ 34 | 35 | boolean hasNullFields = 36 | vrtVatRate.getProductType() == null || 37 | vrtVatRate.getVatRate() == null; 38 | 39 | if(hasNullFields){ 40 | 41 | throw new IllegalFieldException(VrtErrorMessage.FIELDS_CANNOT_BE_NULL); 42 | } 43 | } 44 | 45 | public void controlIsVatRateNegative(VrtVatRate vrtVatRate) { 46 | 47 | if(vrtVatRate.getVatRate()<0){ 48 | throw new IllegalFieldException(VrtErrorMessage.VAT_RATE_CANNOT_BE_NEGATIVE); 49 | } 50 | 51 | } 52 | 53 | public void controlIsProductTypeUnique(VrtVatRate vrtVatRate){ 54 | 55 | VrtProductType vrtProductType = vrtVatRate.getProductType(); 56 | 57 | Optional optionalVrtVatRate = vrtVatRateEntityService.findVatRatesByProductType(vrtProductType); 58 | 59 | VrtVatRate vrtVatRateReturned; 60 | if(optionalVrtVatRate.isPresent()){ 61 | 62 | vrtVatRateReturned = optionalVrtVatRate.get(); 63 | boolean didMatchedItself = didMatchedItself(vrtVatRateReturned, vrtVatRate); 64 | 65 | if(!didMatchedItself){ 66 | throw new ItemAlreadyExistsException(VrtErrorMessage.VAT_RATE_ALREADY_EXIST); 67 | } 68 | 69 | } 70 | } 71 | 72 | private Boolean didMatchedItself(VrtVatRate vrtVatRateReturned, VrtVatRate vrtVatRate){ 73 | 74 | return vrtVatRateReturned.getId().equals(vrtVatRate.getId()); 75 | } 76 | 77 | 78 | } 79 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/java/com/softtech/graduationproject/app/vrt/service/entityservice/VrtVatRateEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.vrt.service.entityservice; 2 | 3 | import com.softtech.graduationproject.app.gen.service.BaseEntityService; 4 | import com.softtech.graduationproject.app.vrt.dao.VrtVatRateDao; 5 | import com.softtech.graduationproject.app.vrt.entity.VrtVatRate; 6 | import com.softtech.graduationproject.app.vrt.enums.VrtProductType; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import java.util.Optional; 11 | 12 | @Service 13 | @Transactional 14 | public class VrtVatRateEntityService extends BaseEntityService { 15 | 16 | public VrtVatRateEntityService(VrtVatRateDao vrtVatRateDao){ 17 | 18 | super(vrtVatRateDao); 19 | } 20 | 21 | public Optional findVatRatesByProductType(VrtProductType vrtProductType){ 22 | 23 | Optional vrtVatRateOptional = getDao().findByProductType(vrtProductType); 24 | 25 | return vrtVatRateOptional; 26 | } 27 | 28 | public Optional findVrtVatRateById(Long id){ 29 | 30 | Optional vrtVatRateOptional = getDao().findVrtVatRateById(id); 31 | 32 | return vrtVatRateOptional; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:postgresql://localhost:5432/softtech-graduation-project 2 | spring.datasource.username=postgres 3 | spring.datasource.password=barorkar99 4 | spring.datasource.driver-class-name=org.postgresql.Driver 5 | 6 | application.title = softtech-graduation-project 7 | application.version = 1.0.0 8 | 9 | server.port = 8080 10 | 11 | logging.level.org.springframework = info 12 | 13 | spring.jpa.show-sql = true 14 | spring.jpa.hibernate.ddl-auto=update 15 | 16 | softtechspringboot.jwt.security.app.key=SOFTTECH 17 | softtechspringboot.jwt.security.expire.time=86400000 -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | _______ _____ _______ _______ _______ _______ _______ _ _ 2 | |______ | | |______ | | |______ | |_____| 3 | ______| |_____| | | | |______ |_____ | | 4 | 5 | ${application.title} ${application.version} 6 | Powered by Spring Boot ${spring-boot.version} -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/test/java/com/softtech/graduationproject/GraduationProjectApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class GraduationProjectApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/test/java/com/softtech/graduationproject/app/BaseTest.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.softtech.graduationproject.app.gen.dto.RestResponse; 5 | import org.springframework.test.web.servlet.MvcResult; 6 | 7 | import java.io.UnsupportedEncodingException; 8 | 9 | public class BaseTest { 10 | 11 | protected ObjectMapper objectMapper; 12 | 13 | protected boolean isSuccess(MvcResult result) throws com.fasterxml.jackson.core.JsonProcessingException, UnsupportedEncodingException { 14 | RestResponse restResponse = getRestResponse(result); 15 | 16 | return isSuccess(restResponse); 17 | } 18 | 19 | protected RestResponse getRestResponse(MvcResult result) throws com.fasterxml.jackson.core.JsonProcessingException, UnsupportedEncodingException { 20 | return objectMapper.readValue(result.getResponse().getContentAsString(), RestResponse.class); 21 | } 22 | 23 | private boolean isSuccess(RestResponse restResponse) { 24 | return restResponse.isSuccess(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/test/java/com/softtech/graduationproject/app/config/H2TestProfileJPAConfig.java: -------------------------------------------------------------------------------- 1 | package com.softtech.graduationproject.app.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.annotation.Profile; 7 | import org.springframework.core.env.Environment; 8 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 9 | import org.springframework.jdbc.datasource.DriverManagerDataSource; 10 | import org.springframework.orm.jpa.JpaTransactionManager; 11 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 12 | import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; 13 | import org.springframework.transaction.annotation.EnableTransactionManagement; 14 | 15 | import javax.persistence.EntityManagerFactory; 16 | import javax.sql.DataSource; 17 | import java.util.Properties; 18 | 19 | @Configuration 20 | @EnableJpaRepositories(basePackages = { "com.softtech.graduationproject", "com.softtech.graduationproject" }) 21 | @EnableTransactionManagement 22 | @Profile("test") 23 | public class H2TestProfileJPAConfig { 24 | 25 | @Autowired 26 | private Environment env; 27 | 28 | @Bean 29 | @Profile("test") 30 | public DataSource dataSource() { 31 | final DriverManagerDataSource dataSource = new DriverManagerDataSource(); 32 | dataSource.setDriverClassName("org.h2.Driver"); 33 | dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1"); 34 | dataSource.setUsername("sa"); 35 | dataSource.setPassword("sa"); 36 | 37 | return dataSource; 38 | } 39 | 40 | @Bean 41 | public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 42 | final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); 43 | em.setDataSource(dataSource()); 44 | em.setPackagesToScan(new String[] { "com.baeldung.boot.domain" }); 45 | em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); 46 | em.setJpaProperties(additionalProperties()); 47 | return em; 48 | } 49 | 50 | @Bean 51 | JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) { 52 | final JpaTransactionManager transactionManager = new JpaTransactionManager(); 53 | transactionManager.setEntityManagerFactory(entityManagerFactory); 54 | return transactionManager; 55 | } 56 | 57 | final Properties additionalProperties() { 58 | final Properties hibernateProperties = new Properties(); 59 | 60 | hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); 61 | hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); 62 | hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql")); 63 | 64 | return hibernateProperties; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /Projects/Bootcamp-Graduation-Project/src/test/java/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:testdb 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password=password 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | spring.h2.console.enabled=true 7 | 8 | spring.jpa.hibernate.ddl-auto=create-drop 9 | spring.jpa.show-sql=true 10 | 11 | application.title = Softtech Java Spring Bootcamp 12 | application.version = 1.0.0 13 | 14 | server.port=8080 15 | 16 | logging.level.org.springframework = error 17 | 18 | softtechspringboot.jwt.security.app.key=SOFTTECH 19 | softtechspringboot.jwt.security.expire.time=86400000 -------------------------------------------------------------------------------- /Projects/Case1/README.md: -------------------------------------------------------------------------------- 1 | # Case1 2 | 3 | 1. Ev, entity.Villa, Yazlık tipinde 3 adet ev tipi oluşturunuz. 4 | 2. Bir class içerisinde en az 3er tane Ev, entity.Villa ve Yazlık nesnelerinden oluşturun ve bu örnekleri size 5 | dönen metotları yazınız. (getEvList, getVillaList vs). 6 | 3. Bir servis class ının içine de aşağıdaki metotları yazınız: 7 | - a. Evlerin toplam fiyatlarını dönen bir metot 8 | - b. Villaların toplam fiyatlarını dönen bir metot 9 | - c. Yazlıkların toplam fiyatlarını dönen metot 10 | - d. Tüm tipteki evlerin toplam fiyatını dönen metot 11 | - e. Evlerin ortalama metrekaresini dönen bir metot 12 | - f. Villaların ortalama metrekaresini dönen bir metot 13 | - g. Yazlıkların ortalama metrekaresini dönen metot 14 | - h. Tüm tipteki evlerin ortalama metrekaresini dönen metot 15 | - i. Oda ve salon sayısına göre tüm tipteki evleri filtreleyip dönen metot 16 | 17 | ## Not: 18 | 19 | 1. İsimlendirmelerin İngilizce olmasına dikkat ediniz. 20 | 2. Nesneye yönelik programlamaya uygun tasarım yapınız. 21 | 3. Clean code prensiplerine uyunuz. 22 | 4. Main metot içinde tüm metotları konsola anlamlı şekilde yazdırınız. 23 | 24 | ### Note 25 | To check commits : https://github.com/165-Softtech-Patika-Java-Spring/case1-cbarkinozer 26 | -------------------------------------------------------------------------------- /Projects/Case1/src/entity/BaseHouse.java: -------------------------------------------------------------------------------- 1 | package entity; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public abstract class BaseHouse { 6 | private BigDecimal price; 7 | private int squareMeters; 8 | private int roomCount; 9 | private int livingRoomCount; 10 | 11 | 12 | public BaseHouse(BigDecimal price, int squareMeters, int roomCount, int livingRoomCount){ 13 | this.price=price; 14 | this.squareMeters=squareMeters; 15 | this.roomCount=roomCount; 16 | this.livingRoomCount=livingRoomCount; 17 | } 18 | 19 | public BigDecimal getPrice() { 20 | return price; 21 | } 22 | 23 | public void setPrice(BigDecimal price) { 24 | this.price = price; 25 | } 26 | 27 | public int getSquareMeters() { 28 | return squareMeters; 29 | } 30 | 31 | public void setSquareMeters(int squareMeters) { 32 | this.squareMeters = squareMeters; 33 | } 34 | 35 | public int getRoomCount() { 36 | return roomCount; 37 | } 38 | 39 | public void setRoomCount(int roomCount) { 40 | this.roomCount = roomCount; 41 | } 42 | 43 | public int getLivingRoomCount() { 44 | return livingRoomCount; 45 | } 46 | 47 | public void setLivingRoomCount(int livingRoomCount) { 48 | this.livingRoomCount = livingRoomCount; 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return "\nprice=" + price + 54 | ", squareMeters=" + squareMeters + 55 | ", roomCount=" + roomCount + 56 | ", livingRoomCount=" + livingRoomCount; 57 | } 58 | } -------------------------------------------------------------------------------- /Projects/Case1/src/entity/House.java: -------------------------------------------------------------------------------- 1 | package entity; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.ArrayList; 5 | 6 | public class House extends BaseHouse { 7 | 8 | private static ArrayList houseList= new ArrayList<>(); 9 | 10 | //Default constructor 11 | public House(){ 12 | super(BigDecimal.valueOf(497500),145,2,1); 13 | addToHouseList(this); 14 | } 15 | 16 | public House(BigDecimal price, int squareMeters, int roomCount, int livingRoomCount){ 17 | super(price, squareMeters, roomCount, livingRoomCount); 18 | addToHouseList(this); 19 | } 20 | 21 | public static ArrayList getHouseList(){ 22 | return houseList; 23 | } 24 | public static void addToHouseList(House house){ 25 | houseList.add(house); 26 | } 27 | } -------------------------------------------------------------------------------- /Projects/Case1/src/entity/HouseDTO.java: -------------------------------------------------------------------------------- 1 | package entity; 2 | 3 | import java.math.BigDecimal; 4 | 5 | public class HouseDTO { 6 | public static String houseType; 7 | public static BigDecimal price; 8 | public static int squareMeters; 9 | public static int roomCount; 10 | public static int livingRoomCount; 11 | 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Projects/Case1/src/entity/HouseFactory.java: -------------------------------------------------------------------------------- 1 | package entity; 2 | 3 | public class HouseFactory { 4 | public void produceHouse(){ 5 | if(HouseDTO.houseType.equalsIgnoreCase("House")){ 6 | new House(HouseDTO.price, HouseDTO.squareMeters, HouseDTO.roomCount, HouseDTO.livingRoomCount); 7 | }else if(HouseDTO.houseType.equalsIgnoreCase("Villa")){ 8 | new Villa(HouseDTO.price, HouseDTO.squareMeters, HouseDTO.roomCount, HouseDTO.livingRoomCount); 9 | }else if(HouseDTO.houseType.equalsIgnoreCase("Summerhouse")){ 10 | new Summerhouse(HouseDTO.price, HouseDTO.squareMeters, HouseDTO.roomCount, HouseDTO.livingRoomCount); 11 | }else{ 12 | System.out.println("Please enter a valid house type"); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Projects/Case1/src/entity/Summerhouse.java: -------------------------------------------------------------------------------- 1 | package entity; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.ArrayList; 5 | 6 | public class Summerhouse extends BaseHouse { 7 | 8 | 9 | private static ArrayList summerhouseList= new ArrayList<>(); 10 | 11 | //Default constructor 12 | public Summerhouse() { 13 | super(BigDecimal.valueOf(3200000),220,4,1); 14 | addToSummerhouseList(this); 15 | } 16 | 17 | public Summerhouse(BigDecimal price, int squareMeters, int roomCount, int livingRoomCount) { 18 | super(price, squareMeters, roomCount, livingRoomCount); 19 | addToSummerhouseList(this); 20 | } 21 | public static ArrayList getSummerhouseList(){ 22 | return summerhouseList; 23 | } 24 | public static void addToSummerhouseList(Summerhouse summerhouse){ 25 | summerhouseList.add(summerhouse); 26 | } 27 | } -------------------------------------------------------------------------------- /Projects/Case1/src/entity/Villa.java: -------------------------------------------------------------------------------- 1 | package entity; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.ArrayList; 5 | 6 | public class Villa extends BaseHouse { 7 | 8 | private static ArrayList villaList= new ArrayList<>(); 9 | 10 | //Default constructor 11 | public Villa() { 12 | super(BigDecimal.valueOf(850000),185,3,1); 13 | addToVillaList(this); 14 | } 15 | 16 | public Villa(BigDecimal price, int squareMeters, int roomCount, int livingRoomCount) { 17 | super(price, squareMeters, roomCount, livingRoomCount); 18 | addToVillaList(this); 19 | } 20 | public static ArrayList getVillaList(){ 21 | return villaList; 22 | } 23 | public static void addToVillaList(Villa villa){ 24 | villaList.add(villa); 25 | } 26 | } -------------------------------------------------------------------------------- /Projects/Case1/src/service/HouseService.java: -------------------------------------------------------------------------------- 1 | package service; 2 | 3 | import entity.BaseHouse; 4 | import entity.House; 5 | import entity.Summerhouse; 6 | import entity.Villa; 7 | 8 | import java.math.BigDecimal; 9 | import java.util.ArrayList; 10 | 11 | public class HouseService{ 12 | 13 | public static BigDecimal sumHousePrices(){ 14 | BigDecimal sumHousePrices= new BigDecimal(0); 15 | for(House house: House.getHouseList()){ 16 | sumHousePrices = sumHousePrices.add(house.getPrice()); 17 | } 18 | return sumHousePrices; 19 | } 20 | 21 | public static BigDecimal sumVillaPrices(){ 22 | BigDecimal sumVillaPrices= new BigDecimal(0); 23 | for(Villa villa: Villa.getVillaList()){ 24 | sumVillaPrices = sumVillaPrices.add(villa.getPrice()); 25 | } 26 | return sumVillaPrices; 27 | } 28 | 29 | public static BigDecimal sumSummerhousePrices(){ 30 | BigDecimal sumSummerhousePrices= new BigDecimal(0); 31 | for(Summerhouse summerhouse: Summerhouse.getSummerhouseList()){ 32 | sumSummerhousePrices = sumSummerhousePrices.add(summerhouse.getPrice()); 33 | } 34 | return sumSummerhousePrices; 35 | } 36 | 37 | public static BigDecimal sumAllPrices(){ 38 | return sumHousePrices().add(sumVillaPrices()).add(sumSummerhousePrices()); 39 | } 40 | 41 | public static double averageHouseSquareMeter(){ 42 | double sumHouseSquareMeter= 0; 43 | for(House house: House.getHouseList()){ 44 | sumHouseSquareMeter+=house.getSquareMeters(); 45 | } 46 | double numberOfElements= House.getHouseList().size(); 47 | return sumHouseSquareMeter/numberOfElements; 48 | } 49 | 50 | public static double averageVillaSquareMeter(){ 51 | double sumVillaSquareMeter=0; 52 | for(Villa villa: Villa.getVillaList()){ 53 | sumVillaSquareMeter+= villa.getSquareMeters(); 54 | } 55 | double numberOfElements = Villa.getVillaList().size(); 56 | return sumVillaSquareMeter/numberOfElements; 57 | } 58 | 59 | public static double averageSummerhouseSquareMeter(){ 60 | double sumSummerhouseSquareMeter=0; 61 | for(Summerhouse summerhouse: Summerhouse.getSummerhouseList()){ 62 | sumSummerhouseSquareMeter+= summerhouse.getSquareMeters(); 63 | } 64 | double numberOfElements = Summerhouse.getSummerhouseList().size(); 65 | return sumSummerhouseSquareMeter/numberOfElements; 66 | } 67 | 68 | public static double averageAllSquareMeter(){ 69 | return (averageHouseSquareMeter()+averageVillaSquareMeter()+averageHouseSquareMeter())/3; 70 | } 71 | 72 | public static ArrayList filterAllByRoomCount(int roomCount,int livingRoomCount){ 73 | ArrayList filteredHouses = new ArrayList<>(); 74 | filteredHouses.addAll( 75 | House.getHouseList().stream() 76 | .filter(x->x.getRoomCount()==roomCount && x.getLivingRoomCount()==livingRoomCount) 77 | .toList() 78 | ); 79 | filteredHouses.addAll( 80 | Villa.getVillaList().stream() 81 | .filter(x->x.getRoomCount()==roomCount && x.getLivingRoomCount()==livingRoomCount) 82 | .toList() 83 | ); 84 | filteredHouses.addAll( 85 | Summerhouse.getSummerhouseList().stream() 86 | .filter(x->x.getRoomCount()==roomCount && x.getLivingRoomCount()==livingRoomCount) 87 | .toList() 88 | ); 89 | return filteredHouses; 90 | } 91 | 92 | } -------------------------------------------------------------------------------- /Projects/Case2/README.md: -------------------------------------------------------------------------------- 1 | # Case2 2 | 3 | ## Adres Kayıt Sistemi: 4 | Bir adreste bulunması gereken alanlar: 5 | - Ülke 6 | - Şehir 7 | - İlçe 8 | - Mahalle 9 | - Sokak 10 | - Kapı No 11 | - Daire No 12 | Bir adet controller yazınız(Tek controller yeterli). Bu controller içerisinde aşağıdaki işlemler yapılabilmelidir. 13 | 1. Ülke kaydedilebilmelidir. ✓ 14 | 2. Ülke kodundan ülke sorgulanabilmelidir. ✓ 15 | 3. Şehir kaydedilebilmelidir. ✓ 16 | 4. Plakadan şehir bilgisi sorgulanabilmelidir. ✓ 17 | 5. İlçe kaydedilebilmelidir. ✓ 18 | 6. Bir ile ait ilçeler sorgulanabilmelidir. ✓ 19 | 7. Mahalle kaydedilebilmelidir. ✓ 20 | 8. Mahalle adını güncellenebilmelidir. ✓ 21 | 9. Bir ilçeye ait mahalleler sorgulanabilmelidir. ✓ 22 | 10. Sokak kaydedilebilmelidir. ✓ 23 | 11. Sokak adı güncellenebilmelidir. ✓ 24 | 12. Bir mahalleye ait sokaklar sorgulanabilmelidir. 25 | 13. Adres kaydedilebilmelidir. ✓ 26 | 14. Adres silinebilmelidir. ✓ 27 | 15. Id den adres bilgisi edinilebilmelidir. ✓ 28 | 29 | ### NOT: 30 | - Address entitysi hariç diğer entitylerin servislerinde entity kullanabilirsiniz. 31 | Yani metot parametresi için dto, return için başka bir dto kullanmanıza gerek yok. 32 | - Address kaydeden dönen vs yerlerde mapper ya da converterlar kullanarak DTO ile veri akışını sağlayınız. ✓ 33 | 34 | ### Design: 35 | Table Design Image: https://ibb.co/Xs8S5Wc 36 | To check commits: https://github.com/165-Softtech-Patika-Java-Spring/case2-cbarkinozer 37 | Note: plateCode renamed as cityCode. 38 | Doors does not have name, they only have doorId. 39 | In City, city Code is cities unique code. E.g 01 for Adana, 872 for chicago, 011 for delhi. 40 | Country has country code that is a unique abbreviation for country. For example: "TR" for Turkey. 41 | 42 | ### TODO: 43 | Fix remaining errors 44 | Test on Swagger 45 | 46 | ## Remaining errors: 47 | 'Many To One' attribute type should not be '****' 24,26,28,30 48 | -------------------------------------------------------------------------------- /Projects/Case2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.6.3 9 | 10 | 11 | com.softtech 12 | softtech-spring-boot 13 | 0.0.1-SNAPSHOT 14 | softtech-spring-boot 15 | Demo project for Spring Boot 16 | 17 | 11 18 | 1.4.2.Final 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-data-jpa 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-hateoas 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-validation 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-devtools 41 | runtime 42 | true 43 | 44 | 45 | com.h2database 46 | h2 47 | runtime 48 | 49 | 50 | org.projectlombok 51 | lombok 52 | true 53 | 54 | 55 | org.springdoc 56 | springdoc-openapi-ui 57 | 1.6.6 58 | 59 | 60 | 61 | org.mapstruct 62 | mapstruct 63 | ${org.mapstruct.version} 64 | 65 | 66 | 67 | org.mapstruct 68 | mapstruct-processor 69 | ${org.mapstruct.version} 70 | 71 | 72 | 73 | org.springframework.boot 74 | spring-boot-starter-test 75 | test 76 | 77 | 78 | 79 | 80 | 81 | 82 | org.springframework.boot 83 | spring-boot-maven-plugin 84 | 85 | 86 | 87 | org.project-lombok 88 | lombok 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/SofttechSpringBootApplication.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SofttechSpringBootApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SofttechSpringBootApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/add/controller/AddAddressController.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.add.controller; 2 | 3 | 4 | import com.softtech.softtechspringboot.add.dto.AddAddressDto; 5 | import com.softtech.softtechspringboot.add.dto.AddAddressSaveRequestDto; 6 | import com.softtech.softtechspringboot.add.service.AddAddressService; 7 | import lombok.RequiredArgsConstructor; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.util.List; 12 | 13 | @RestController 14 | @RequestMapping("/api/v1/addresses") 15 | @RequiredArgsConstructor 16 | public class AddAddressController { 17 | 18 | private final AddAddressService addAddressService; 19 | 20 | @GetMapping 21 | public ResponseEntity findAll(){ 22 | List addAddressDaoList=addAddressService.findAll(); 23 | return ResponseEntity.ok(addAddressDaoList); 24 | } 25 | 26 | //15. find Address by Id. 27 | @GetMapping("/{id}") 28 | public ResponseEntity findById(@PathVariable Long id){ 29 | AddAddressDto addAddressDto =addAddressService.findById(id); 30 | return ResponseEntity.ok(addAddressDto); 31 | } 32 | 33 | //13. Address is savable. 34 | @PostMapping 35 | public ResponseEntity save(@RequestBody AddAddressSaveRequestDto addAddressSaveRequestDto){ 36 | AddAddressDto addAddressDto = addAddressService.save(addAddressSaveRequestDto); 37 | return ResponseEntity.ok(addAddressDto); 38 | } 39 | //14. Address is deletable . 40 | @DeleteMapping("/{id}") 41 | public ResponseEntity delete(@PathVariable Long id){ 42 | addAddressService.delete(id); 43 | return ResponseEntity.ok(Void.TYPE); 44 | } 45 | @PutMapping 46 | public ResponseEntity update(@RequestBody AddAddressDto addAddressDto){ 47 | AddAddressDto addAddressDtoUpdated = addAddressService.update(addAddressDto); 48 | return ResponseEntity.ok(addAddressDtoUpdated); 49 | } 50 | 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/add/converter/AddAddressMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.add.converter; 2 | 3 | import com.softtech.softtechspringboot.add.dto.AddAddressDto; 4 | import com.softtech.softtechspringboot.add.dto.AddAddressSaveRequestDto; 5 | import com.softtech.softtechspringboot.add.entity.AddAddress; 6 | import org.mapstruct.Mapper; 7 | import org.mapstruct.ReportingPolicy; 8 | import org.mapstruct.factory.Mappers; 9 | 10 | import java.util.List; 11 | 12 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 13 | public interface AddAddressMapper { 14 | AddAddressMapper INSTANCE = Mappers.getMapper(AddAddressMapper.class); 15 | 16 | AddAddress convertToAddAddress(AddAddressDto addAddressDto); 17 | AddAddress convertToAddAddress(AddAddressSaveRequestDto addAddressSaveRequestDto); 18 | List convertToAddAddressDtoList(List addAddressList); 19 | AddAddressDto convertToAddAddressDto(AddAddress addAddress); 20 | } 21 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/add/dao/AddAddressDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.add.dao; 2 | 3 | import com.softtech.softtechspringboot.add.entity.AddAddress; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface AddAddressDao extends JpaRepository { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/add/dto/AddAddressDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.add.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class AddAddressDto { 7 | private Long id; 8 | private Long countryId; 9 | private Long cityId; 10 | private Long districtId; 11 | private Long neighborhoodId; 12 | private Long streetId; 13 | private Long buildingId; 14 | private Long doorId; 15 | } 16 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/add/dto/AddAddressSaveRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.add.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class AddAddressSaveRequestDto { 7 | private Long countryId; 8 | private Long cityId; 9 | private Long districtId; 10 | private Long neighborhoodId; 11 | private Long streetId; 12 | private Long buildingId; 13 | private Long doorId; 14 | } 15 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/add/entity/AddAddress.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.add.entity; 2 | import com.softtech.softtechspringboot.bld.entity.BldBuilding; 3 | import com.softtech.softtechspringboot.cnt.entity.CntCountry; 4 | import com.softtech.softtechspringboot.cty.entity.CtyCity; 5 | import com.softtech.softtechspringboot.dor.entity.DorDoor; 6 | import com.softtech.softtechspringboot.dst.entity.DstDistrict; 7 | import com.softtech.softtechspringboot.ngh.entity.NghNeighborhood; 8 | import com.softtech.softtechspringboot.str.entity.StrStreet; 9 | import lombok.Data; 10 | 11 | import javax.persistence.*; 12 | import java.util.HashSet; 13 | import java.util.Set; 14 | 15 | @Entity 16 | @Table(name="ADD_ADDRESS") 17 | @Data 18 | public class AddAddress { 19 | @Id 20 | @SequenceGenerator(name="AddAddress",sequenceName = "ADD_ADDRESS_ID_SEQ") 21 | @GeneratedValue(generator="AddAddress") 22 | private Long id; 23 | @ManyToOne 24 | private CntCountry cntCountry; 25 | @ManyToOne 26 | private CtyCity ctyCity; 27 | @ManyToOne 28 | private DstDistrict dstDistrict; 29 | @ManyToOne 30 | private NghNeighborhood nghNeighborhood; 31 | @ManyToOne 32 | private StrStreet strStreet; 33 | @ManyToOne 34 | private BldBuilding bldBuilding; 35 | @ManyToOne 36 | private DorDoor dorDoor; 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/add/enums/AddErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.add.enums; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | 5 | public enum AddErrorMessage implements BaseErrorMessage { 6 | 7 | ADDRESS_ERROR_MESSAGE("Address not found!") 8 | ; 9 | 10 | private String message; 11 | 12 | AddErrorMessage(String message){ 13 | this.message=message; 14 | } 15 | 16 | public String getMessage() { 17 | return message; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return message; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/add/service/AddAddressService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.add.service; 2 | 3 | import com.softtech.softtechspringboot.add.converter.AddAddressMapper; 4 | import com.softtech.softtechspringboot.add.dto.AddAddressDto; 5 | import com.softtech.softtechspringboot.add.dto.AddAddressSaveRequestDto; 6 | import com.softtech.softtechspringboot.add.entity.AddAddress; 7 | import com.softtech.softtechspringboot.add.enums.AddErrorMessage; 8 | import com.softtech.softtechspringboot.add.service.entityservice.AddAddressEntityService; 9 | import com.softtech.softtechspringboot.cnt.converter.CntCountryMapper; 10 | import com.softtech.softtechspringboot.cnt.entity.CntCountry; 11 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 12 | import lombok.RequiredArgsConstructor; 13 | import org.springframework.stereotype.Service; 14 | import java.util.List; 15 | 16 | @Service 17 | @RequiredArgsConstructor 18 | public class AddAddressService { 19 | 20 | private final AddAddressEntityService addAddressEntityService; 21 | 22 | 23 | public List findAll() { 24 | List addAddressList = addAddressEntityService.findAll(); 25 | return AddAddressMapper.INSTANCE.convertToAddAddressDtoList(addAddressList); 26 | } 27 | 28 | 29 | public AddAddressDto findById(Long id) { 30 | AddAddress addAddress = addAddressEntityService.getByIdWithControl(id); 31 | return AddAddressMapper.INSTANCE.convertToAddAddressDto(addAddress); 32 | 33 | } 34 | 35 | 36 | public AddAddressDto save(AddAddressSaveRequestDto addAddressSaveRequestDto) { 37 | AddAddress addAddress = AddAddressMapper.INSTANCE.convertToAddAddress(addAddressSaveRequestDto); 38 | addAddress = addAddressEntityService.save(addAddress); 39 | return AddAddressMapper.INSTANCE.convertToAddAddressDto(addAddress); 40 | } 41 | 42 | public void delete(Long id) { 43 | AddAddress addAddress = addAddressEntityService.getByIdWithControl(id); 44 | addAddressEntityService.delete(addAddress); 45 | } 46 | 47 | public AddAddressDto update(AddAddressDto addAddressDto) { 48 | controlIsAddressExist(addAddressDto); 49 | AddAddress updateAddress = AddAddressMapper.INSTANCE.convertToAddAddress(addAddressDto); 50 | updateAddress = addAddressEntityService.save(updateAddress); 51 | return AddAddressMapper.INSTANCE.convertToAddAddressDto(updateAddress); 52 | } 53 | private void controlIsAddressExist(AddAddressDto addAddressDto) { 54 | Long id = addAddressDto.getId(); 55 | boolean isExist = addAddressEntityService.existsById(id); 56 | if (!isExist){ 57 | throw new ItemNotFoundException(AddErrorMessage.ADDRESS_ERROR_MESSAGE); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/add/service/entityservice/AddAddressEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.add.service.entityservice; 2 | 3 | import com.softtech.softtechspringboot.add.dao.AddAddressDao; 4 | import com.softtech.softtechspringboot.add.entity.AddAddress; 5 | import com.softtech.softtechspringboot.gen.service.BaseEntityService; 6 | import org.springframework.stereotype.Service; 7 | 8 | @Service 9 | public class AddAddressEntityService extends BaseEntityService { 10 | 11 | public AddAddressEntityService(AddAddressDao addAddressDao) { 12 | super(addAddressDao); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/bld/dao/BldBuildingDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.bld.dao; 2 | 3 | import com.softtech.softtechspringboot.bld.entity.BldBuilding; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface BldBuildingDao extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/bld/dto/BldBuildingDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.bld.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class BldBuildingDto { 7 | 8 | private Long id; 9 | private Long buildingId; 10 | private Long buildingName; 11 | } 12 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/bld/entity/BldBuilding.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.bld.entity; 2 | 3 | import com.softtech.softtechspringboot.str.entity.StrStreet; 4 | import lombok.Data; 5 | 6 | import javax.persistence.*; 7 | 8 | @Data 9 | @Entity 10 | @Table(name="BLD_BUILDING") 11 | public class BldBuilding { 12 | @Id 13 | @SequenceGenerator(name="BldBuilding",sequenceName = "BLD_BUILDING_ID_SEQ") 14 | @GeneratedValue(generator="BldBuilding") 15 | private Long id; 16 | @Column(name="BLD_BUILDING_ID",nullable = false) 17 | private Long buildingId; 18 | 19 | @Column(name="BLD_BUILDING_NAME",nullable = true) 20 | private Long buildingName; 21 | 22 | @ManyToOne( 23 | fetch= FetchType.LAZY, 24 | cascade=CascadeType.ALL, 25 | optional=false) 26 | private StrStreet strStreet; 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cnt/controller/CntCountryController.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cnt.controller; 2 | 3 | import com.softtech.softtechspringboot.cnt.dto.CntCountryDto; 4 | import com.softtech.softtechspringboot.cnt.dto.CntCountrySaveRequestDto; 5 | import com.softtech.softtechspringboot.cnt.service.CntCountryService; 6 | import lombok.RequiredArgsConstructor; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | @RestController 11 | @RequestMapping("/api/v1/countries") 12 | @RequiredArgsConstructor 13 | public class CntCountryController { 14 | private final CntCountryService cntCountryService; 15 | 16 | //1. Save Country 17 | @PostMapping 18 | public ResponseEntity save(@RequestBody CntCountrySaveRequestDto cntCountrySaveRequestDto){ 19 | CntCountryDto cntCountryDto = cntCountryService.save(cntCountrySaveRequestDto); 20 | return ResponseEntity.ok(cntCountryDto); 21 | } 22 | //2. Query country by country code 23 | @GetMapping("/{countryCode}") 24 | public ResponseEntity findByCountryCode(@PathVariable String countryCode){ 25 | CntCountryDto cntCountryDto =cntCountryService.findByCountryCode(countryCode); 26 | return ResponseEntity.ok(cntCountryDto); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cnt/converter/CntCountryMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cnt.converter; 2 | 3 | import com.softtech.softtechspringboot.cnt.dto.CntCountryDto; 4 | import com.softtech.softtechspringboot.cnt.dto.CntCountrySaveRequestDto; 5 | import com.softtech.softtechspringboot.cnt.entity.CntCountry; 6 | import org.mapstruct.Mapper; 7 | import org.mapstruct.ReportingPolicy; 8 | import org.mapstruct.factory.Mappers; 9 | 10 | import java.util.List; 11 | 12 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 13 | public interface CntCountryMapper { 14 | CntCountryMapper INSTANCE = Mappers.getMapper(CntCountryMapper.class); 15 | 16 | CntCountry convertToCntCountry(CntCountryDto cntCountryDto); 17 | CntCountry convertToCntCountry(CntCountrySaveRequestDto cntCountrySaveRequestDto); 18 | List convertToCntCountryDtoList(List cntCountryList); 19 | CntCountryDto convertToCntCountryDto(CntCountry cntCountry); 20 | } 21 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cnt/dao/CntCountryDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cnt.dao; 2 | 3 | import com.softtech.softtechspringboot.cnt.entity.CntCountry; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.data.jpa.repository.Query; 6 | import org.springframework.data.repository.query.Param; 7 | import org.springframework.stereotype.Repository; 8 | 9 | 10 | @Repository 11 | public interface CntCountryDao extends JpaRepository { 12 | CntCountry getCountryByCountryCode(String countryCode); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cnt/dto/CntCountryDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cnt.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class CntCountryDto { 7 | private Long id; 8 | private Long countryId; 9 | private String countryName; 10 | private String countryCode; 11 | } 12 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cnt/dto/CntCountrySaveRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cnt.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class CntCountrySaveRequestDto { 7 | private Long countryId; 8 | private String countryName; 9 | private String countryCode; 10 | } 11 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cnt/entity/CntCountry.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cnt.entity; 2 | 3 | import com.softtech.softtechspringboot.cty.entity.CtyCity; 4 | import lombok.Data; 5 | import lombok.RequiredArgsConstructor; 6 | 7 | import javax.persistence.*; 8 | import java.util.List; 9 | 10 | @Data 11 | @RequiredArgsConstructor 12 | @Table(name="CNT_COUNTRY") 13 | public class CntCountry { 14 | @Id 15 | @SequenceGenerator(name="CntCountry",sequenceName = "CNT_COUNTRY_ID_SEQ") 16 | @GeneratedValue(generator="CntCountry") 17 | private Long id; 18 | 19 | @Column(name="CNT_COUNTRY_ID",nullable = false) 20 | private Long countryId; 21 | 22 | @Column(name="CTY_CITY_NAME",nullable = false) 23 | private String countryName; 24 | 25 | @Column(name="CNT_COUNTRY_CODE",length=5, nullable = false) 26 | private String countryCode; 27 | 28 | @OneToMany(mappedBy = "CntCountry",fetch = FetchType.LAZY,cascade = CascadeType.ALL) 29 | private List cities; 30 | } 31 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cnt/enums/CntErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cnt.enums; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | 5 | public enum CntErrorMessage implements BaseErrorMessage { 6 | 7 | COUNTRY_ERROR_MESSAGE("Country not found!") 8 | ; 9 | 10 | private String message; 11 | 12 | CntErrorMessage(String message){ 13 | this.message=message; 14 | } 15 | 16 | public String getMessage() { 17 | return message; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return message; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cnt/service/CntCountryService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cnt.service; 2 | 3 | import com.softtech.softtechspringboot.cnt.converter.CntCountryMapper; 4 | import com.softtech.softtechspringboot.cnt.dto.CntCountryDto; 5 | import com.softtech.softtechspringboot.cnt.dto.CntCountrySaveRequestDto; 6 | import com.softtech.softtechspringboot.cnt.entity.CntCountry; 7 | import com.softtech.softtechspringboot.cnt.service.entityservice.CntCountryEntityService; 8 | import com.softtech.softtechspringboot.cty.converter.CtyCityMapper; 9 | import com.softtech.softtechspringboot.cty.dto.CtyCityDto; 10 | import com.softtech.softtechspringboot.cty.entity.CtyCity; 11 | import com.softtech.softtechspringboot.cty.service.entityservice.CtyCityEntityService; 12 | import lombok.RequiredArgsConstructor; 13 | import org.springframework.stereotype.Service; 14 | 15 | @Service 16 | @RequiredArgsConstructor 17 | public class CntCountryService { 18 | private final CntCountryEntityService cntCountryEntityService; 19 | public CntCountryDto save(CntCountrySaveRequestDto cntCountrySaveRequestDto) { 20 | CntCountry cntCountry = CntCountryMapper.INSTANCE.convertToCntCountry(cntCountrySaveRequestDto); 21 | cntCountry = cntCountryEntityService.save(cntCountry); 22 | return CntCountryMapper.INSTANCE.convertToCntCountryDto(cntCountry); 23 | } 24 | 25 | public CntCountryDto findByCountryCode(String countryCode) { 26 | CntCountry cntCountry = cntCountryEntityService.getByCountryCodeWithControl(countryCode); 27 | return CntCountryMapper.INSTANCE.convertToCntCountryDto(cntCountry); 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cnt/service/entityservice/CntCountryEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cnt.service.entityservice; 2 | 3 | import com.softtech.softtechspringboot.cnt.dao.CntCountryDao; 4 | import com.softtech.softtechspringboot.cnt.entity.CntCountry; 5 | import com.softtech.softtechspringboot.cnt.enums.CntErrorMessage; 6 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 7 | import com.softtech.softtechspringboot.gen.service.BaseEntityService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | 12 | @Service 13 | public class CntCountryEntityService extends BaseEntityService { 14 | private final CntCountryDao cntCountryDao; 15 | @Autowired 16 | public CntCountryEntityService(CntCountryDao cntCountryDao) { 17 | super(cntCountryDao); 18 | this.cntCountryDao = cntCountryDao; 19 | } 20 | 21 | public CntCountry getByCountryCodeWithControl(String countryCode) { 22 | CntCountry cntCountry= cntCountryDao.getCountryByCountryCode(countryCode); 23 | if (cntCountry==null){ 24 | throw new ItemNotFoundException(CntErrorMessage.COUNTRY_ERROR_MESSAGE); 25 | } 26 | return cntCountry; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cty/controller/CtyCityController.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cty.controller; 2 | 3 | import com.softtech.softtechspringboot.cty.dto.CtyCityDto; 4 | import com.softtech.softtechspringboot.cty.dto.CtyCitySaveRequestDto; 5 | import com.softtech.softtechspringboot.cty.service.CtyCityService; 6 | import lombok.RequiredArgsConstructor; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | @RestController 11 | @RequestMapping("/api/v1/cities") 12 | @RequiredArgsConstructor 13 | public class CtyCityController { 14 | private final CtyCityService ctyCityService; 15 | //3. City is savable 16 | public ResponseEntity save(@RequestBody CtyCitySaveRequestDto ctyCitySaveRequestDto){ 17 | CtyCityDto ctyCityDto = ctyCityService.save(ctyCitySaveRequestDto); 18 | return ResponseEntity.ok(ctyCityDto); 19 | } 20 | 21 | //4. Query city from plateCode(cityCode) 22 | @GetMapping("/{cityCode}") 23 | public ResponseEntity findByCityCode(@PathVariable Long cityCode){ 24 | CtyCityDto ctyCityDto =ctyCityService.findByCityCode(cityCode); 25 | return ResponseEntity.ok(ctyCityDto); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cty/converter/CtyCityMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cty.converter; 2 | 3 | import com.softtech.softtechspringboot.cty.dto.CtyCityDto; 4 | import com.softtech.softtechspringboot.cty.dto.CtyCitySaveRequestDto; 5 | import com.softtech.softtechspringboot.cty.entity.CtyCity; 6 | import org.mapstruct.Mapper; 7 | import org.mapstruct.ReportingPolicy; 8 | import org.mapstruct.factory.Mappers; 9 | 10 | import java.util.List; 11 | 12 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 13 | public interface CtyCityMapper { 14 | CtyCityMapper INSTANCE = Mappers.getMapper(CtyCityMapper.class); 15 | 16 | CtyCity convertToCtyCity(CtyCityDto ctyCityDto); 17 | CtyCity convertToCtyCity(CtyCitySaveRequestDto ctyCitySaveRequestDto); 18 | List convertToCtyCityDtoList(List ctyCityList); 19 | CtyCityDto convertToCtyCityDto(CtyCity ctyCity); 20 | } 21 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cty/dao/CtyCityDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cty.dao; 2 | 3 | import com.softtech.softtechspringboot.cty.entity.CtyCity; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | 8 | @Repository 9 | public interface CtyCityDao extends JpaRepository { 10 | CtyCity getCityByPlateCode(String plateCode); 11 | } 12 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cty/dto/CtyCityDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cty.dto; 2 | 3 | import lombok.Data; 4 | 5 | 6 | @Data 7 | public class CtyCityDto { 8 | private Long id; 9 | private Long cityId; 10 | private String cityName; 11 | private String cityCode; 12 | } 13 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cty/dto/CtyCitySaveRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cty.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class CtyCitySaveRequestDto { 7 | private Long cityId; 8 | private String cityName; 9 | private String cityCode; 10 | } 11 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cty/entity/CtyCity.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cty.entity; 2 | 3 | import com.softtech.softtechspringboot.cnt.entity.CntCountry; 4 | import com.softtech.softtechspringboot.dst.entity.DstDistrict; 5 | import lombok.Data; 6 | import lombok.RequiredArgsConstructor; 7 | 8 | import javax.persistence.*; 9 | import java.util.List; 10 | 11 | @Data 12 | @RequiredArgsConstructor 13 | @Table(name="CTY_CITY") 14 | public class CtyCity { 15 | @Id 16 | @SequenceGenerator(name="CtyCity",sequenceName = "CTY_CITY_ID_SEQ") 17 | @GeneratedValue(generator="CtyCity") 18 | private Long id; 19 | 20 | @Column(name="CTY_CITY_ID",nullable = false) 21 | private Long cityId; 22 | 23 | @Column(name="CTY_CITY_NAME",nullable = false) 24 | private String cityName; 25 | 26 | @Column(name="CTY_CITY_CODE",nullable = false) 27 | private Long cityCode; 28 | 29 | 30 | @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY) 31 | private CntCountry cntCountry; 32 | 33 | @OneToMany(mappedBy = "city",fetch = FetchType.LAZY) 34 | private List dstDistrictList; 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cty/enums/CtyErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cty.enums; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | 5 | public enum CtyErrorMessage implements BaseErrorMessage { 6 | 7 | CITY_ERROR_MESSAGE("City not found!") 8 | ; 9 | 10 | private String message; 11 | 12 | CtyErrorMessage(String message){ 13 | this.message=message; 14 | } 15 | 16 | public String getMessage() { 17 | return message; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return message; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cty/service/CtyCityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cty.service; 2 | 3 | import com.softtech.softtechspringboot.cty.converter.CtyCityMapper; 4 | import com.softtech.softtechspringboot.cty.dto.CtyCityDto; 5 | import com.softtech.softtechspringboot.cty.dto.CtyCitySaveRequestDto; 6 | import com.softtech.softtechspringboot.cty.entity.CtyCity; 7 | import com.softtech.softtechspringboot.cty.enums.CtyErrorMessage; 8 | import com.softtech.softtechspringboot.cty.service.entityservice.CtyCityEntityService; 9 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 10 | import lombok.RequiredArgsConstructor; 11 | import org.springframework.stereotype.Service; 12 | 13 | @Service 14 | @RequiredArgsConstructor 15 | public class CtyCityService { 16 | private final CtyCityEntityService ctyCityEntityService; 17 | 18 | public CtyCityDto save(CtyCitySaveRequestDto ctyCitySaveRequestDto) { 19 | CtyCity ctyCity = CtyCityMapper.INSTANCE.convertToCtyCity(ctyCitySaveRequestDto); 20 | ctyCity = ctyCityEntityService.save(ctyCity); 21 | return CtyCityMapper.INSTANCE.convertToCtyCityDto(ctyCity); 22 | } 23 | 24 | public CtyCityDto findByCityCode(Long cityCode) { 25 | CtyCity ctyCity = ctyCityEntityService.getByIdWithControl(cityCode); 26 | return CtyCityMapper.INSTANCE.convertToCtyCityDto(ctyCity); 27 | } 28 | 29 | public CtyCity getById(long id){ 30 | return ctyCityEntityService.findById(id).orElseThrow(() -> new ItemNotFoundException(CtyErrorMessage.CITY_ERROR_MESSAGE)); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/cty/service/entityservice/CtyCityEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cty.service.entityservice; 2 | 3 | 4 | import com.softtech.softtechspringboot.cty.dao.CtyCityDao; 5 | import com.softtech.softtechspringboot.cty.entity.CtyCity; 6 | import com.softtech.softtechspringboot.gen.service.BaseEntityService; 7 | import org.springframework.stereotype.Service; 8 | 9 | 10 | @Service 11 | public class CtyCityEntityService extends BaseEntityService { 12 | public CtyCityEntityService(CtyCityDao ctyCityDao) { 13 | super(ctyCityDao); 14 | } 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dor/dao/DorDoorDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dor.dao; 2 | 3 | import com.softtech.softtechspringboot.dor.entity.DorDoor; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface DorDoorDao extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dor/dto/DorDoorDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dor.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class DorDoorDto { 7 | 8 | private Long id; 9 | private Long DoorId; 10 | } 11 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dor/entity/DorDoor.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dor.entity; 2 | 3 | import com.softtech.softtechspringboot.bld.entity.BldBuilding; 4 | import lombok.Data; 5 | 6 | import javax.persistence.*; 7 | 8 | @Entity 9 | @Table(name="DOR_DOOR") 10 | @Data 11 | public class DorDoor { 12 | @Id 13 | @SequenceGenerator(name="DorDoor",sequenceName = "DOR_DOOR_ID_SEQ") 14 | @GeneratedValue(generator="DorDoor") 15 | private Long id; 16 | 17 | @Column(name="DOR_DOOR_ID",nullable = false) 18 | private Long buildingId; 19 | 20 | @Column(name="DOR_DOOR_NAME",nullable = true) 21 | private String buildingName; 22 | 23 | @ManyToOne( 24 | fetch= FetchType.LAZY, 25 | cascade=CascadeType.ALL, 26 | optional=false) 27 | private BldBuilding bldBuilding; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dor/service/DorDoorService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dor.service; 2 | 3 | import com.softtech.softtechspringboot.dor.service.entityservice.DorDoorEntityService; 4 | import lombok.RequiredArgsConstructor; 5 | import org.springframework.stereotype.Service; 6 | 7 | @Service 8 | @RequiredArgsConstructor 9 | public class DorDoorService { 10 | private final DorDoorEntityService dorDoorEntityService; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dor/service/entityservice/DorDoorEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dor.service.entityservice; 2 | 3 | import com.softtech.softtechspringboot.dor.dao.DorDoorDao; 4 | import com.softtech.softtechspringboot.dor.entity.DorDoor; 5 | import com.softtech.softtechspringboot.gen.service.BaseEntityService; 6 | import org.springframework.stereotype.Service; 7 | 8 | @Service 9 | public class DorDoorEntityService extends BaseEntityService { 10 | 11 | public DorDoorEntityService(DorDoorDao dorDoorDao) { 12 | super(dorDoorDao); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dst/controller/DstDistrictController.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dst.controller; 2 | 3 | import com.softtech.softtechspringboot.dst.dto.DstDistrictDto; 4 | import com.softtech.softtechspringboot.dst.dto.DstDistrictSaveRequestDto; 5 | import com.softtech.softtechspringboot.dst.service.DstDistrictService; 6 | import lombok.RequiredArgsConstructor; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | @RestController 11 | @RequestMapping("/api/v1/districts") 12 | @RequiredArgsConstructor 13 | public class DstDistrictController { 14 | private final DstDistrictService dstDistrictService; 15 | 16 | //5. District is savable 17 | @PostMapping 18 | public ResponseEntity save(@RequestBody DstDistrictSaveRequestDto dstDistrictSaveRequestDto){ 19 | DstDistrictDto dstDistrictDto = dstDistrictService.save(dstDistrictSaveRequestDto); 20 | return ResponseEntity.ok(dstDistrictDto); 21 | } 22 | //6. Query districts that belongs to a city 23 | @GetMapping("/{cityId}") 24 | public ResponseEntity findByDistrictId(@PathVariable Long cityId){ 25 | DstDistrictDto dstDistrictDto =dstDistrictService.findByCityId(cityId); 26 | return ResponseEntity.ok(dstDistrictDto); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dst/converter/DstDistrictMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dst.converter; 2 | 3 | import com.softtech.softtechspringboot.dst.dto.DstDistrictDto; 4 | import com.softtech.softtechspringboot.dst.dto.DstDistrictSaveRequestDto; 5 | import com.softtech.softtechspringboot.dst.entity.DstDistrict; 6 | import org.mapstruct.Mapper; 7 | import org.mapstruct.ReportingPolicy; 8 | import org.mapstruct.factory.Mappers; 9 | 10 | import java.util.List; 11 | 12 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 13 | public interface DstDistrictMapper { 14 | DstDistrictMapper INSTANCE = Mappers.getMapper(DstDistrictMapper.class); 15 | 16 | DstDistrict convertToDstDistrict(DstDistrictDto dstDistrictDto); 17 | DstDistrict convertToDstDistrict(DstDistrictSaveRequestDto DstDistrictSaveRequestDto); 18 | List convertToDstDistrictDtoList(List DstDistrictList); 19 | DstDistrictDto convertToDstDistrictDto(DstDistrict dstDistrict); 20 | } 21 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dst/dao/DstDistrictDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dst.dao; 2 | 3 | import com.softtech.softtechspringboot.dst.entity.DstDistrict; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface DstDistrictDao extends JpaRepository { 9 | DstDistrict getDistrictByCityCode(Long cityId); 10 | } 11 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dst/dto/DstDistrictDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dst.dto; 2 | 3 | import lombok.Data; 4 | 5 | 6 | @Data 7 | public class DstDistrictDto { 8 | private Long id; 9 | private Long districtId; 10 | private String districtName; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dst/dto/DstDistrictSaveRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dst.dto; 2 | 3 | public class DstDistrictSaveRequestDto { 4 | private Long districtId; 5 | private String districtName; 6 | } 7 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dst/entity/DstDistrict.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dst.entity; 2 | 3 | import com.softtech.softtechspringboot.cty.entity.CtyCity; 4 | import com.softtech.softtechspringboot.ngh.entity.NghNeighborhood; 5 | import lombok.Data; 6 | import lombok.RequiredArgsConstructor; 7 | 8 | import javax.persistence.*; 9 | import java.util.List; 10 | 11 | @Data 12 | @RequiredArgsConstructor 13 | @Table(name="DST_DISTRICT") 14 | public class DstDistrict { 15 | @Id 16 | @SequenceGenerator(name="DstDistrict",sequenceName = "DST_DISTRICT_ID_SEQ") 17 | @GeneratedValue(generator="DstDistrict") 18 | private Long id; 19 | 20 | @Column(name="DST_DISTRICT_ID",nullable = false) 21 | private Long districtId; 22 | 23 | @Column(name="DST_DISTRICT_NAME",nullable = false) 24 | private String districtName; 25 | @ManyToOne 26 | private CtyCity ctyCity; 27 | 28 | @OneToMany(mappedBy = "DstDistrict",fetch = FetchType.LAZY) 29 | private List nghNeighborhoodList; 30 | } 31 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dst/enums/DstErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dst.enums; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | 5 | public enum DstErrorMessage implements BaseErrorMessage { 6 | 7 | DISTRICT_ERROR_MESSAGE("District not found!") 8 | ; 9 | 10 | private String message; 11 | 12 | DstErrorMessage(String message){ 13 | this.message=message; 14 | } 15 | 16 | public String getMessage() { 17 | return message; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return message; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dst/service/DstDistrictService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dst.service; 2 | 3 | import com.softtech.softtechspringboot.dst.converter.DstDistrictMapper; 4 | import com.softtech.softtechspringboot.dst.dto.DstDistrictDto; 5 | import com.softtech.softtechspringboot.dst.dto.DstDistrictSaveRequestDto; 6 | import com.softtech.softtechspringboot.dst.entity.DstDistrict; 7 | import com.softtech.softtechspringboot.dst.service.entityservice.DstDistrictEntityService; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.stereotype.Service; 10 | 11 | @Service 12 | @RequiredArgsConstructor 13 | public class DstDistrictService { 14 | private final DstDistrictEntityService dstDistrictEntityService; 15 | 16 | public DstDistrictDto save(DstDistrictSaveRequestDto dstDistrictSaveRequestDto) { 17 | DstDistrict dstDistrict = DstDistrictMapper.INSTANCE.convertToDstDistrict(dstDistrictSaveRequestDto); 18 | dstDistrict = dstDistrictEntityService.save(dstDistrict); 19 | return DstDistrictMapper.INSTANCE.convertToDstDistrictDto(dstDistrict); 20 | } 21 | 22 | public DstDistrictDto findByCityId(Long cityId) { 23 | DstDistrict dstDistrict = dstDistrictEntityService.getByCityCodeWithControl(cityId); 24 | return DstDistrictMapper.INSTANCE.convertToDstDistrictDto(dstDistrict); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/dst/service/entityservice/DstDistrictEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.dst.service.entityservice; 2 | 3 | 4 | import com.softtech.softtechspringboot.dst.dao.DstDistrictDao; 5 | import com.softtech.softtechspringboot.dst.entity.DstDistrict; 6 | import com.softtech.softtechspringboot.dst.enums.DstErrorMessage; 7 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 8 | import com.softtech.softtechspringboot.gen.service.BaseEntityService; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Service; 11 | 12 | 13 | @Service 14 | public class DstDistrictEntityService extends BaseEntityService { 15 | private final DstDistrictDao dstDistrictDao; 16 | @Autowired 17 | public DstDistrictEntityService(DstDistrictDao dstDistrictDao) { 18 | super(dstDistrictDao); 19 | this.dstDistrictDao=dstDistrictDao; 20 | } 21 | 22 | public DstDistrict getByCityCodeWithControl(Long cityId) { 23 | DstDistrict dstDistrict= dstDistrictDao.getDistrictByCityCode(cityId); 24 | if (dstDistrict==null){ 25 | throw new ItemNotFoundException(DstErrorMessage.DISTRICT_ERROR_MESSAGE); 26 | } 27 | return dstDistrict; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/gen/enums/BaseErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.enums; 2 | 3 | public interface BaseErrorMessage { 4 | 5 | String getMessage(); 6 | } 7 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/gen/enums/GenErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.enums; 2 | 3 | public enum GenErrorMessage implements BaseErrorMessage{ 4 | 5 | ITEM_NOT_FOUND("Item not found!"), 6 | ; 7 | 8 | private String message; 9 | GenErrorMessage(String message) { 10 | this.message = message; 11 | } 12 | 13 | public String getMessage() { 14 | return message; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return message; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/gen/exception/GenCustomizedResponseEntityExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.exception; 2 | 3 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 4 | import org.springframework.http.HttpHeaders; 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.MethodArgumentNotValidException; 8 | import org.springframework.web.bind.annotation.ControllerAdvice; 9 | import org.springframework.web.bind.annotation.ExceptionHandler; 10 | import org.springframework.web.bind.annotation.RestController; 11 | import org.springframework.web.context.request.WebRequest; 12 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 13 | 14 | import java.util.Date; 15 | 16 | @RestController 17 | @ControllerAdvice 18 | public class GenCustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { 19 | 20 | @ExceptionHandler 21 | public final ResponseEntity handleAllExceptions(Exception ex, WebRequest webRequest){ 22 | 23 | Date errorDate = new Date(); 24 | String message = ex.getMessage(); 25 | String description = webRequest.getDescription(false); 26 | 27 | GenExceptionResponse genExceptionResponse = new GenExceptionResponse(errorDate, message, description); 28 | 29 | return new ResponseEntity<>(genExceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR); 30 | } 31 | 32 | @ExceptionHandler 33 | public final ResponseEntity handleAllItemNotFoundException(ItemNotFoundException ex, WebRequest webRequest){ 34 | 35 | Date errorDate = new Date(); 36 | String message = ex.getMessage(); 37 | String description = webRequest.getDescription(false); 38 | 39 | GenExceptionResponse genExceptionResponse = new GenExceptionResponse(errorDate, message, description); 40 | 41 | return new ResponseEntity<>(genExceptionResponse, HttpStatus.NOT_FOUND); 42 | } 43 | 44 | @Override 45 | protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { 46 | 47 | Date errorDate = new Date(); 48 | String message = "Validation failed!"; 49 | String description = ex.getBindingResult().toString(); 50 | 51 | GenExceptionResponse genExceptionResponse = new GenExceptionResponse(errorDate, message, description); 52 | 53 | return new ResponseEntity<>(genExceptionResponse, HttpStatus.BAD_REQUEST); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/gen/exception/GenExceptionResponse.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.exception; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | import java.util.Date; 7 | 8 | @Data 9 | @AllArgsConstructor 10 | public class GenExceptionResponse { 11 | 12 | private Date errorDate; 13 | private String message; 14 | private String detail; 15 | } 16 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/gen/exceptions/ItemNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.exceptions; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.web.bind.annotation.ResponseStatus; 6 | 7 | @ResponseStatus(HttpStatus.NOT_FOUND) 8 | public class ItemNotFoundException extends RuntimeException { 9 | public ItemNotFoundException(BaseErrorMessage message) { 10 | super(message.getMessage()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/gen/service/BaseEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.service; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.GenErrorMessage; 4 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 5 | import lombok.RequiredArgsConstructor; 6 | import org.springframework.data.jpa.repository.JpaRepository; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | import java.util.Optional; 11 | 12 | @Service 13 | @RequiredArgsConstructor 14 | public class BaseEntityService> { 15 | 16 | private final D dao; 17 | 18 | public List findAll(){ 19 | return dao.findAll(); 20 | } 21 | 22 | public Optional findById(Long id){ 23 | return dao.findById(id); 24 | } 25 | 26 | public E save(E entity){ 27 | return dao.save(entity); 28 | } 29 | 30 | public void delete(E entity){ 31 | dao.delete(entity); 32 | } 33 | 34 | public E getByIdWithControl(Long id) { 35 | 36 | Optional entityOptional = findById(id); 37 | 38 | E entity; 39 | if (entityOptional.isPresent()){ 40 | entity = entityOptional.get(); 41 | } else { 42 | throw new ItemNotFoundException(GenErrorMessage.ITEM_NOT_FOUND); 43 | } 44 | 45 | return entity; 46 | } 47 | 48 | public boolean existsById(Long id){ 49 | return dao.existsById(id); 50 | } 51 | 52 | public D getDao() { 53 | return dao; 54 | } 55 | } -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/ngh/controller/NghNeighborhoodController.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.ngh.controller; 2 | 3 | 4 | import com.softtech.softtechspringboot.ngh.dto.NghNeighborhoodDto; 5 | import com.softtech.softtechspringboot.ngh.dto.NghNeighborhoodSaveRequestDto; 6 | import com.softtech.softtechspringboot.ngh.service.NghNeighborhoodService; 7 | import lombok.RequiredArgsConstructor; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | @RestController 12 | @RequiredArgsConstructor 13 | @RequestMapping("/api/v1/neighborhoods") 14 | public class NghNeighborhoodController { 15 | private final NghNeighborhoodService nghNeighborhoodService; 16 | 17 | //7. Neighborhood is savable 18 | @PostMapping 19 | public ResponseEntity save(@RequestBody NghNeighborhoodSaveRequestDto nghNeighborhoodSaveRequestDto){ 20 | NghNeighborhoodDto nghNeighborhoodDto = nghNeighborhoodService.save(nghNeighborhoodSaveRequestDto); 21 | return ResponseEntity.ok(nghNeighborhoodDto); 22 | } 23 | //8. Neighborhood name is updatable 24 | @PutMapping 25 | public ResponseEntity update(@RequestBody NghNeighborhoodDto nghNeighborhoodDto){ 26 | NghNeighborhoodDto nghNeighborhoodDtoUpdated = nghNeighborhoodService.update(nghNeighborhoodDto); 27 | return ResponseEntity.ok(nghNeighborhoodDtoUpdated); 28 | } 29 | //9. Query neighborhoods that belongs to a district 30 | @GetMapping("/{districtId}") 31 | public ResponseEntity findByDistrictId(@PathVariable Long districtId){ 32 | NghNeighborhoodDto nghNeighborhoodDto =nghNeighborhoodService.findByDistrictId(districtId); 33 | return ResponseEntity.ok(nghNeighborhoodDto); 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/ngh/converter/NghNeighborhoodMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.ngh.converter; 2 | 3 | import com.softtech.softtechspringboot.add.converter.AddAddressMapper; 4 | import com.softtech.softtechspringboot.add.dto.AddAddressDto; 5 | import com.softtech.softtechspringboot.add.dto.AddAddressSaveRequestDto; 6 | import com.softtech.softtechspringboot.add.entity.AddAddress; 7 | import com.softtech.softtechspringboot.ngh.dto.NghNeighborhoodDto; 8 | import com.softtech.softtechspringboot.ngh.dto.NghNeighborhoodSaveRequestDto; 9 | import com.softtech.softtechspringboot.ngh.entity.NghNeighborhood; 10 | import org.mapstruct.Mapper; 11 | import org.mapstruct.ReportingPolicy; 12 | import org.mapstruct.factory.Mappers; 13 | 14 | import java.util.List; 15 | 16 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 17 | public interface NghNeighborhoodMapper { 18 | NghNeighborhoodMapper INSTANCE = Mappers.getMapper(NghNeighborhoodMapper.class); 19 | 20 | NghNeighborhood convertToNghNeighborhood(NghNeighborhoodDto nghNeighborhoodDto); 21 | NghNeighborhood convertToNghNeighborhood(NghNeighborhoodSaveRequestDto nghNeighborhoodSaveRequestDto); 22 | List convertToNeighborhoodDtoList(List nghNeighborhoodList); 23 | NghNeighborhoodDto convertToNghNeighborhoodDto(NghNeighborhood nghNeighborhood); 24 | } 25 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/ngh/dao/NghNeighborhoodDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.ngh.dao; 2 | 3 | import com.softtech.softtechspringboot.ngh.entity.NghNeighborhood; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.data.jpa.repository.Query; 6 | import org.springframework.data.repository.query.Param; 7 | import org.springframework.stereotype.Repository; 8 | 9 | import java.util.List; 10 | 11 | @Repository 12 | public interface NghNeighborhoodDao extends JpaRepository { 13 | NghNeighborhood getNeighborhoodByDistrictId(long districtId); 14 | } 15 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/ngh/dto/NghNeighborhoodDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.ngh.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class NghNeighborhoodDto { 7 | private Long id; 8 | private Long neighborhoodId; 9 | private String neighborhoodName; 10 | } 11 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/ngh/dto/NghNeighborhoodSaveRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.ngh.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class NghNeighborhoodSaveRequestDto { 7 | private Long neighborhoodId; 8 | private String neighborhoodName; 9 | } 10 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/ngh/entity/NghNeighborhood.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.ngh.entity; 2 | 3 | import com.softtech.softtechspringboot.dst.entity.DstDistrict; 4 | import com.softtech.softtechspringboot.str.entity.StrStreet; 5 | import lombok.Data; 6 | import lombok.RequiredArgsConstructor; 7 | 8 | import javax.persistence.*; 9 | import java.util.List; 10 | 11 | @Data 12 | @Table(name="NGH_NEIGHBORHOOD") 13 | @RequiredArgsConstructor 14 | public class NghNeighborhood { 15 | @Id 16 | @SequenceGenerator(name="NghNeighborhood",sequenceName = "NGH_NEIGHBORHOOD_ID_SEQ") 17 | @GeneratedValue(generator="NghNeighborhood") 18 | private Long id; 19 | 20 | @Column(name="NGH_NEIGHBORHOOD_ID",nullable = false) 21 | private Long neighborhoodId; 22 | 23 | @Column(name="NGH_NEIGHBORHOOD_NAME",nullable = false) 24 | private String neighborhoodName; 25 | 26 | @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY) 27 | private DstDistrict dstDistrict; 28 | 29 | @OneToMany(mappedBy = "NghNeighborhood",fetch = FetchType.LAZY) 30 | private List strStreetList; 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/ngh/enums/NghErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.ngh.enums; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | 5 | public enum NghErrorMessage implements BaseErrorMessage { 6 | NEIGHBORHOOD_ERROR_MESSAGE("Neighborhood not found!") 7 | ; 8 | 9 | private String message; 10 | 11 | NghErrorMessage(String message){ 12 | this.message=message; 13 | } 14 | 15 | public String getMessage() { 16 | return message; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return message; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/ngh/service/NghNeighborhoodService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.ngh.service; 2 | 3 | import com.softtech.softtechspringboot.add.enums.AddErrorMessage; 4 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 5 | import com.softtech.softtechspringboot.ngh.converter.NghNeighborhoodMapper; 6 | import com.softtech.softtechspringboot.ngh.dto.NghNeighborhoodDto; 7 | import com.softtech.softtechspringboot.ngh.dto.NghNeighborhoodSaveRequestDto; 8 | import com.softtech.softtechspringboot.ngh.entity.NghNeighborhood; 9 | import com.softtech.softtechspringboot.ngh.service.entityservice.NghNeighborhoodEntityService; 10 | import lombok.RequiredArgsConstructor; 11 | import org.springframework.stereotype.Service; 12 | 13 | @Service 14 | @RequiredArgsConstructor 15 | public class NghNeighborhoodService { 16 | private final NghNeighborhoodEntityService nghNeighborhoodEntityService; 17 | 18 | public NghNeighborhoodDto save(NghNeighborhoodSaveRequestDto nghNeighborhoodSaveRequestDto) { 19 | NghNeighborhood nghNeighborhood = NghNeighborhoodMapper.INSTANCE.convertToNghNeighborhood(nghNeighborhoodSaveRequestDto); 20 | nghNeighborhood = nghNeighborhoodEntityService.save(nghNeighborhood); 21 | return NghNeighborhoodMapper.INSTANCE.convertToNghNeighborhoodDto(nghNeighborhood); 22 | } 23 | 24 | public NghNeighborhoodDto update(NghNeighborhoodDto nghNeighborhoodDto) { 25 | controlIsNeighborhoodExist(nghNeighborhoodDto); 26 | NghNeighborhood updateNeighborhood = NghNeighborhoodMapper.INSTANCE.convertToNghNeighborhood(nghNeighborhoodDto); 27 | updateNeighborhood = nghNeighborhoodEntityService.save(updateNeighborhood); 28 | return NghNeighborhoodMapper.INSTANCE.convertToNghNeighborhoodDto(updateNeighborhood); 29 | } 30 | 31 | private void controlIsNeighborhoodExist(NghNeighborhoodDto nghNeighborhoodDto) { 32 | Long id = nghNeighborhoodDto.getId(); 33 | boolean isExist = nghNeighborhoodEntityService.existsById(id); 34 | if (!isExist){ 35 | throw new ItemNotFoundException(AddErrorMessage.ADDRESS_ERROR_MESSAGE); 36 | } 37 | } 38 | 39 | public NghNeighborhoodDto findByDistrictId(Long id) { 40 | NghNeighborhoodDto nghNeighborhoodDto = nghNeighborhoodEntityService.getByDistrictIdWithControl(id); 41 | return nghNeighborhoodDto ; 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/ngh/service/entityservice/NghNeighborhoodEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.ngh.service.entityservice; 2 | 3 | import com.softtech.softtechspringboot.cnt.enums.CntErrorMessage; 4 | import com.softtech.softtechspringboot.dst.entity.DstDistrict; 5 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 6 | import com.softtech.softtechspringboot.gen.service.BaseEntityService; 7 | import com.softtech.softtechspringboot.ngh.converter.NghNeighborhoodMapper; 8 | import com.softtech.softtechspringboot.ngh.dao.NghNeighborhoodDao; 9 | import com.softtech.softtechspringboot.ngh.dto.NghNeighborhoodDto; 10 | import com.softtech.softtechspringboot.ngh.entity.NghNeighborhood; 11 | import com.softtech.softtechspringboot.ngh.enums.NghErrorMessage; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.stereotype.Service; 14 | 15 | import java.util.List; 16 | 17 | @Service 18 | public class NghNeighborhoodEntityService extends BaseEntityService { 19 | private final NghNeighborhoodDao nghNeighborhoodDao; 20 | @Autowired 21 | public NghNeighborhoodEntityService(NghNeighborhoodDao nghNeighborhoodDao) { 22 | super(nghNeighborhoodDao); 23 | this.nghNeighborhoodDao=nghNeighborhoodDao; 24 | } 25 | 26 | public NghNeighborhoodDto getByDistrictIdWithControl(Long districtId) { 27 | NghNeighborhood nghNeighborhood = nghNeighborhoodDao.getNeighborhoodByDistrictId(districtId); 28 | return NghNeighborhoodMapper.INSTANCE.convertToNghNeighborhoodDto(nghNeighborhood); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/str/controller/StrStreetController.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.str.controller; 2 | 3 | 4 | 5 | import com.softtech.softtechspringboot.str.dto.StrStreetDto; 6 | import com.softtech.softtechspringboot.str.dto.StrStreetSaveRequestDto; 7 | import com.softtech.softtechspringboot.str.service.StrStreetService; 8 | import lombok.RequiredArgsConstructor; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.*; 11 | 12 | @RestController 13 | @RequiredArgsConstructor 14 | @RequestMapping("/api/v1/streets") 15 | public class StrStreetController { 16 | private final StrStreetService strStreetService; 17 | 18 | //10. Street is savable 19 | @PostMapping 20 | public ResponseEntity save(@RequestBody StrStreetSaveRequestDto strStreetSaveRequestDto){ 21 | StrStreetDto strStreetDto = strStreetService.save(strStreetSaveRequestDto); 22 | return ResponseEntity.ok(strStreetDto); 23 | } 24 | 25 | //11. Street name is updatable 26 | @PutMapping 27 | public ResponseEntity update(@RequestBody StrStreetDto strStreetDto){ 28 | StrStreetDto strStreetDtoUpdated = strStreetService.update(strStreetDto); 29 | return ResponseEntity.ok(strStreetDtoUpdated); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/str/converter/StrStreetMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.str.converter; 2 | 3 | 4 | import com.softtech.softtechspringboot.str.dto.StrStreetDto; 5 | import com.softtech.softtechspringboot.str.dto.StrStreetSaveRequestDto; 6 | import com.softtech.softtechspringboot.str.entity.StrStreet; 7 | import org.mapstruct.Mapper; 8 | import org.mapstruct.ReportingPolicy; 9 | import org.mapstruct.factory.Mappers; 10 | 11 | import java.util.List; 12 | 13 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 14 | public interface StrStreetMapper { 15 | StrStreetMapper INSTANCE = Mappers.getMapper(StrStreetMapper.class); 16 | 17 | StrStreet convertToStrStreet(StrStreetDto strStreetDto); 18 | StrStreet convertToStrStreet(StrStreetSaveRequestDto strStreetSaveRequestDto); 19 | List convertToStrStreetDtoList(List strStreetList); 20 | StrStreetDto convertToStrStreetDto(StrStreet strStreet); 21 | } 22 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/str/dao/StrStreetDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.str.dao; 2 | 3 | import com.softtech.softtechspringboot.str.entity.StrStreet; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.List; 8 | 9 | @Repository 10 | public interface StrStreetDao extends JpaRepository { 11 | List getStreetByNeighborhoodId(long neighborhoodId); 12 | } 13 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/str/dto/StrStreetDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.str.dto; 2 | 3 | import lombok.Data; 4 | 5 | 6 | @Data 7 | public class StrStreetDto { 8 | private Long id; 9 | private Long streetId; 10 | private String streetName; 11 | } 12 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/str/dto/StrStreetSaveRequestDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.str.dto; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class StrStreetSaveRequestDto { 7 | private Long streetId; 8 | private String streetName; 9 | } 10 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/str/entity/StrStreet.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.str.entity; 2 | 3 | import com.softtech.softtechspringboot.ngh.entity.NghNeighborhood; 4 | import lombok.Data; 5 | 6 | import javax.persistence.*; 7 | 8 | @Data 9 | @Entity 10 | @Table(name="STR_STREET") 11 | public class StrStreet { 12 | @Id 13 | @SequenceGenerator(name="StrStreet",sequenceName = "STR_STREET_ID_SEQ") 14 | @GeneratedValue(generator="StrStreet") 15 | private Long id; 16 | @Column(name="STR_STREET_ID",nullable = false) 17 | private String streetId; 18 | @Column(name="STR_STREET_NAME",nullable = true) 19 | private String streetName; 20 | 21 | @ManyToOne( 22 | fetch= FetchType.LAZY, 23 | cascade=CascadeType.ALL, 24 | optional=false) 25 | 26 | private NghNeighborhood nghNeighborhood; 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/str/enums/StrErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.str.enums; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | 5 | public enum StrErrorMessage implements BaseErrorMessage { 6 | STREET_ERROR_MESSAGE("Street not found!") 7 | ; 8 | 9 | private String message; 10 | 11 | StrErrorMessage(String message){ 12 | this.message=message; 13 | } 14 | 15 | public String getMessage() { 16 | return message; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return message; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/str/service/StrStreetService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.str.service; 2 | 3 | 4 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 5 | import com.softtech.softtechspringboot.str.converter.StrStreetMapper; 6 | import com.softtech.softtechspringboot.str.dto.StrStreetDto; 7 | import com.softtech.softtechspringboot.str.dto.StrStreetSaveRequestDto; 8 | import com.softtech.softtechspringboot.str.entity.StrStreet; 9 | import com.softtech.softtechspringboot.str.enums.StrErrorMessage; 10 | import com.softtech.softtechspringboot.str.service.entityservice.StrStreetEntityService; 11 | import lombok.RequiredArgsConstructor; 12 | import org.springframework.stereotype.Service; 13 | 14 | @Service 15 | @RequiredArgsConstructor 16 | public class StrStreetService { 17 | private final StrStreetEntityService strStreetEntityService; 18 | 19 | 20 | public StrStreetDto save(StrStreetSaveRequestDto strStreetSaveRequestDto) { 21 | StrStreet strStreet = StrStreetMapper.INSTANCE.convertToStrStreet(strStreetSaveRequestDto); 22 | strStreet = strStreetEntityService.save(strStreet); 23 | return StrStreetMapper.INSTANCE.convertToStrStreetDto(strStreet); 24 | } 25 | 26 | public StrStreetDto update(StrStreetDto strStreetDto) { 27 | controlIsStreetExist(strStreetDto); 28 | StrStreet updateStreet = StrStreetMapper.INSTANCE.convertToStrStreet(strStreetDto); 29 | updateStreet = strStreetEntityService.save(updateStreet); 30 | return StrStreetMapper.INSTANCE.convertToStrStreetDto(updateStreet); 31 | } 32 | private void controlIsStreetExist(StrStreetDto strStreetDto) { 33 | Long id = strStreetDto.getId(); 34 | boolean isExist = strStreetEntityService.existsById(id); 35 | if (!isExist){ 36 | throw new ItemNotFoundException(StrErrorMessage.STREET_ERROR_MESSAGE); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/java/com/softtech/softtechspringboot/str/service/entityservice/StrStreetEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.str.service.entityservice; 2 | 3 | 4 | import com.softtech.softtechspringboot.gen.service.BaseEntityService; 5 | import com.softtech.softtechspringboot.str.dao.StrStreetDao; 6 | import com.softtech.softtechspringboot.str.entity.StrStreet; 7 | import org.springframework.stereotype.Service; 8 | 9 | @Service 10 | public class StrStreetEntityService extends BaseEntityService { 11 | public StrStreetEntityService(StrStreetDao strStreetDao) { 12 | super(strStreetDao); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Projects/Case2/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:testdb 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password=password 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | 7 | application.title = case-2 8 | application.version = 1.0.0 9 | 10 | server.port = 8080 11 | 12 | logging.level.org.springframework = info 13 | 14 | spring.jpa.show-sql = true 15 | spring.jpa.hibernate.ddl-auto=create -------------------------------------------------------------------------------- /Projects/Case2/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | _______ _____ _______ _______ _______ _______ _______ _ _ 2 | |______ | | |______ | | |______ | |_____| 3 | ______| |_____| | | | |______ |_____ | | 4 | 5 | ${application.title} ${application.version} 6 | Powered by Spring Boot ${spring-boot.version} -------------------------------------------------------------------------------- /Projects/Case2/src/test/java/com/softtech/softtechspringboot/SofttechSpringBootApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SofttechSpringBootApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Projects/Case3/README.md: -------------------------------------------------------------------------------- 1 | # Case3 2 | 3 | Bir online alışveriş sitesinin kullanıcı yorumlarını yöneten bir api yazınız. 4 | - Bir kullanıcının sadece bir e-maili ve bir telefon numarası olabilir. ✓ 5 | - Kullanıcı tipi kişi ya da kurum olabilir. ✓ 6 | 1. **Ürün controller ında**; 7 | - Ürün kaydeden bir servis yazınız.(save) ✓ 8 | - Ürünleri listeleyebilen bir servis yazınız.(findAll) ✓ 9 | - Ürün idsinden ürünü bulan bir servis yazınız.(findById) ✓ 10 | - Ürün silen bir metot yazınız.(delete) ✓ 11 | - Ürün fiyatı güncelleyen bir metot yazınız. (Sadece fiyat) (updatePrice) ✓ 12 | 13 | 2. **Kullanıcı controller ında;** 14 | - Tüm kullanıcıları getiren bir servis yazınız. (findAll) ✓ 15 | - Kullanıcı idsinden kullanıcıyı getiren bir servis yazınız. (findById) ✓ 16 | - Kullanıcı adından kullanıcıyı getiren bir servis yazınız. (findByUsername) ✓ 17 | - Kullanıcı kaydedilebilecek bir servis yazınız. (save) ✓ 18 | - Kullanıcı aynı kullanıcı adı, telefon ya da e-mail ile kayıt yapamaz. ✓ 19 | - Kullanıcı silebilecek bir servis yazınız. (kullanıcı adı, ve telefon bilgileri alınmalı). (delete) ✓ 20 | - Eğer kullanıcı adı ve telefon uyuşmuyorsa “XXX kullanıcı adı ile YYY telefonu bilgileri uyuşmamaktadır.” 21 | şeklinde bir uyarı vermeli. 22 | - Kullanıcı bilgilerini güncelleyebilecek bir servis yazınız. (update) ✓ 23 | 24 | 3. **Yorum Controller ında;** 25 | - Bir kullanıcının yaptığı yorumlari getiren bir servis yazınız. Eğer ilgili kullanıcının henüz bir 26 | yorumu yoksa “XXX kullanıcı henüz bir yorum yazmamıştır” şeklinde bir hata vermeli. (findByUserId) ✓ 27 | - Bir ürüne yapılan tüm yorumları getiren bir servis yazınız. Eğer o ürüne henüz bir yorum (findAllByProductId) ✓ 28 | yazılmamışsa “XXX ürüne henüz bir yorum yazılmamıştır” şeklinde bir hata vermeli. 29 | - Yeni bir yorum yazılabilecek bir servis yazınız. (save) ✓ 30 | - Yorum silebilecek bir servis yazınız. (delete) ✓ 31 | 32 | #### Notlar: 33 | Yukarıdaki hiçbir servis Persistance nesne almamalı ya da dönmemelidir.(dto ve mapper kullan) ✓ 34 | Pathlere dikkat ediniz. ✓ 35 | Dao ve entityService deki method isimlerine dikkat ediniz. ✓ 36 | Dto isimlendirmeleri, fieldları vs size bırakıyorum. ✓ 37 | Spring Data JPA da sorgu yazmak ile alakalı ya da otomatik sorguların nasıl yapıldığı ile alakalı da ufak 38 | bir araştırma yapmanız gerekecek. ✓ 39 | 40 | # Notes 41 | Database table design : https://ibb.co/wpbn6DF 42 | All works on Swagger 43 | To check commits : https://github.com/165-Softtech-Patika-Java-Spring/case3-cbarkinozer 44 | -------------------------------------------------------------------------------- /Projects/Case3/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.6.3 9 | 10 | 11 | com.softtech 12 | softtech-spring-boot 13 | 0.0.1-SNAPSHOT 14 | softtech-spring-boot 15 | Demo project for Spring Boot 16 | 17 | 16 18 | 1.4.2.Final 19 | 20 | 21 | 22 | org.springframework.boot 23 | spring-boot-starter-data-jpa 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-hateoas 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-validation 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-devtools 41 | runtime 42 | true 43 | 44 | 45 | com.h2database 46 | h2 47 | runtime 48 | 49 | 50 | org.projectlombok 51 | lombok 52 | true 53 | 54 | 55 | org.springdoc 56 | springdoc-openapi-ui 57 | 1.6.6 58 | 59 | 60 | 61 | org.mapstruct 62 | mapstruct 63 | ${org.mapstruct.version} 64 | 65 | 66 | 67 | org.mapstruct 68 | mapstruct-processor 69 | ${org.mapstruct.version} 70 | 71 | 72 | org.springframework.boot 73 | spring-boot-starter-test 74 | test 75 | 76 | 77 | 78 | 79 | 80 | 81 | org.springframework.boot 82 | spring-boot-maven-plugin 83 | 84 | 85 | 86 | org.projectlombok 87 | lombok 88 | 89 | 90 | 91 | 92 | 93 | org.apache.maven.plugins 94 | maven-compiler-plugin 95 | 96 | 16 97 | 16 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/SofttechSpringBootApplication.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SofttechSpringBootApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SofttechSpringBootApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/cmt/controller/CmtCommentController.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cmt.controller; 2 | 3 | import com.softtech.softtechspringboot.cmt.dto.CmtCommentDto; 4 | import com.softtech.softtechspringboot.cmt.service.CmtCommentService; 5 | import com.softtech.softtechspringboot.gen.dto.RestResponse; 6 | import io.swagger.v3.oas.annotations.Operation; 7 | import lombok.RequiredArgsConstructor; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.util.List; 12 | 13 | @RestController 14 | @RequiredArgsConstructor 15 | @RequestMapping("api/v1/comments") 16 | public class CmtCommentController { 17 | private final CmtCommentService cmtCommentService; 18 | 19 | @Operation(summary="Get a comment by user id.") 20 | @GetMapping("/user-id/{userId}") 21 | public ResponseEntity findByUserId(@PathVariable Long userId){ 22 | 23 | CmtCommentDto cmtCommentDto = cmtCommentService.findByUserId(userId); 24 | return ResponseEntity.ok(RestResponse.of(cmtCommentDto)); 25 | 26 | } 27 | 28 | @Operation(summary="Get all comments by product id.") 29 | @GetMapping("/product-id/{productId}") 30 | public ResponseEntity findAllByProductId(@PathVariable Long productId){ 31 | 32 | List cmtCommentDtoList = cmtCommentService.findAllByProductId(productId); 33 | return ResponseEntity.ok(RestResponse.of(cmtCommentDtoList)); 34 | 35 | } 36 | 37 | @Operation(summary="Save a comment.") 38 | @PostMapping("/save") 39 | public ResponseEntity save(@RequestBody CmtCommentDto cmtCommentDto){ 40 | 41 | CmtCommentDto cmtCommentDtoSaved = cmtCommentService.save(cmtCommentDto); 42 | return ResponseEntity.ok(RestResponse.of(cmtCommentDtoSaved)); 43 | 44 | } 45 | 46 | @Operation(summary="Delete a comment.") 47 | @DeleteMapping("/delete/{id}") 48 | public ResponseEntity delete(@PathVariable Long id) { 49 | 50 | cmtCommentService.delete(id); 51 | return ResponseEntity.ok(RestResponse.empty()); 52 | 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/cmt/converter/CmtCommentMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cmt.converter; 2 | 3 | import com.softtech.softtechspringboot.cmt.dto.CmtCommentDto; 4 | import com.softtech.softtechspringboot.cmt.entity.CmtComment; 5 | import org.mapstruct.Mapper; 6 | import org.mapstruct.ReportingPolicy; 7 | import org.mapstruct.factory.Mappers; 8 | 9 | import java.util.List; 10 | 11 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 12 | public interface CmtCommentMapper { 13 | 14 | CmtCommentMapper INSTANCE = Mappers.getMapper(CmtCommentMapper.class); 15 | 16 | CmtComment convertToCmtComment(CmtCommentDto cmtCommentDto); 17 | List convertToCmtCommentDtoList(List cmtCommentList); 18 | CmtCommentDto convertToCmtCommentDto(CmtComment cmtComment); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/cmt/dao/CmtCommentDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cmt.dao; 2 | 3 | import com.softtech.softtechspringboot.cmt.entity.CmtComment; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.List; 8 | import java.util.Optional; 9 | 10 | @Repository 11 | public interface CmtCommentDao extends JpaRepository { 12 | 13 | Optional findByUserId(Long userId); 14 | List findAllByProductId(Long productId); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/cmt/dto/CmtCommentDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cmt.dto; 2 | 3 | import lombok.Data; 4 | 5 | import javax.validation.constraints.NotNull; 6 | 7 | 8 | @Data 9 | public class CmtCommentDto { 10 | 11 | @NotNull 12 | private String comment; 13 | 14 | @NotNull 15 | private Long productId; 16 | 17 | @NotNull 18 | private Long userId; 19 | } 20 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/cmt/entity/CmtComment.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cmt.entity; 2 | 3 | 4 | import com.softtech.softtechspringboot.gen.entity.BaseEntity; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import javax.persistence.*; 9 | 10 | @Entity 11 | @Table(name="CMT_COMMENT") 12 | @Getter @Setter 13 | public class CmtComment extends BaseEntity { 14 | 15 | @Id 16 | @SequenceGenerator(name ="CMT_COMMENT" ,sequenceName = "CMT_COMMENT_ID_SEQ") 17 | @GeneratedValue(generator ="CMT_COMMENT") 18 | private Long id; 19 | 20 | @Lob 21 | @Column(name="COMMENT",nullable = false) 22 | private String comment; 23 | 24 | @Column(name="ID_PRD_PRODUCT",nullable = false) 25 | private Long productId; 26 | 27 | @Column(name="ID_USR_USER",nullable = false) 28 | private Long userId; 29 | } 30 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/cmt/enums/CmtErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cmt.enums; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | 5 | public enum CmtErrorMessage implements BaseErrorMessage { 6 | 7 | COMMENT_NOT_FOUND("Comment not found!"), 8 | COMMENT_IS_BLANK("Comment is blank!"), 9 | USER_HAS_NO_COMMENT("user has no comment!"), 10 | PRODUCT_HAS_NO_COMMENT("product has no comment!") 11 | ; 12 | 13 | private String message; 14 | 15 | CmtErrorMessage(String message) { 16 | this.message = message; 17 | } 18 | 19 | public String getMessage() { 20 | return message; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return message; 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/cmt/service/CmtCommentService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cmt.service; 2 | 3 | import com.softtech.softtechspringboot.cmt.converter.CmtCommentMapper; 4 | import com.softtech.softtechspringboot.cmt.dto.CmtCommentDto; 5 | import com.softtech.softtechspringboot.cmt.entity.CmtComment; 6 | import com.softtech.softtechspringboot.cmt.service.entityservice.CmtCommentEntityService; 7 | import lombok.RequiredArgsConstructor; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.util.List; 11 | 12 | @Service 13 | @RequiredArgsConstructor 14 | public class CmtCommentService { 15 | 16 | private final CmtCommentEntityService cmtCommentEntityService; 17 | 18 | public CmtCommentDto findByUserId(Long userId) { 19 | 20 | CmtComment cmtComment = cmtCommentEntityService.getByUserIdWithControl(userId); 21 | return CmtCommentMapper.INSTANCE.convertToCmtCommentDto(cmtComment); 22 | 23 | } 24 | 25 | 26 | public List findAllByProductId(Long productId) { 27 | 28 | List cmtCommentList = cmtCommentEntityService.getAllByProductId(productId); 29 | return CmtCommentMapper.INSTANCE.convertToCmtCommentDtoList(cmtCommentList); 30 | 31 | } 32 | 33 | 34 | public CmtCommentDto save(CmtCommentDto cmtCommentDto) { 35 | CmtComment cmtComment = CmtCommentMapper.INSTANCE.convertToCmtComment(cmtCommentDto); 36 | cmtComment = cmtCommentEntityService.save(cmtComment); 37 | return CmtCommentMapper.INSTANCE.convertToCmtCommentDto(cmtComment); 38 | } 39 | 40 | public void delete(Long id) { 41 | cmtCommentEntityService.delete(id); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/cmt/service/entityservice/CmtCommentEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.cmt.service.entityservice; 2 | 3 | import com.softtech.softtechspringboot.cmt.dao.CmtCommentDao; 4 | import com.softtech.softtechspringboot.cmt.entity.CmtComment; 5 | import com.softtech.softtechspringboot.cmt.enums.CmtErrorMessage; 6 | import com.softtech.softtechspringboot.gen.exceptions.IllegalFieldException; 7 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 8 | import com.softtech.softtechspringboot.gen.service.BaseEntityService; 9 | import com.softtech.softtechspringboot.prd.service.entityservice.PrdProductEntityService; 10 | import com.softtech.softtechspringboot.usr.service.entityservice.UsrUserEntityService; 11 | import org.springframework.stereotype.Service; 12 | 13 | 14 | import java.util.List; 15 | 16 | @Service 17 | public class CmtCommentEntityService extends BaseEntityService { 18 | 19 | private final CmtCommentDao cmtCommentDao; 20 | private final UsrUserEntityService usrUserEntityService; 21 | private final PrdProductEntityService prdProductEntityService; 22 | 23 | public CmtCommentEntityService(CmtCommentDao cmtCommentDao, 24 | UsrUserEntityService usrUserEntityService, 25 | PrdProductEntityService prdProductEntityService) { 26 | super(cmtCommentDao); 27 | this.cmtCommentDao = cmtCommentDao; 28 | this.usrUserEntityService=usrUserEntityService; 29 | this.prdProductEntityService = prdProductEntityService; 30 | } 31 | 32 | public CmtComment getByUserIdWithControl(Long userId) { 33 | 34 | return checkIfCommentExistsByUserId(userId); 35 | 36 | } 37 | 38 | private CmtComment checkIfCommentExistsByUserId(Long userId) { 39 | 40 | CmtComment cmtComment = cmtCommentDao.findByUserId(userId) 41 | .orElseThrow(() -> new ItemNotFoundException( 42 | usrUserEntityService.findByIdWithControl(userId).getUsername(), 43 | CmtErrorMessage.USER_HAS_NO_COMMENT) ); 44 | 45 | return cmtComment; 46 | } 47 | 48 | 49 | public List getAllByProductId(Long productId) { 50 | 51 | List cmtCommentList = cmtCommentDao.findAllByProductId(productId); 52 | if(cmtCommentList.isEmpty()){ 53 | throw new ItemNotFoundException( 54 | prdProductEntityService.findById(productId).getName(), 55 | CmtErrorMessage.PRODUCT_HAS_NO_COMMENT); 56 | }else{ 57 | return cmtCommentList; 58 | } 59 | 60 | } 61 | 62 | public CmtComment save(CmtComment cmtComment) { 63 | controlIsCommentBlank(cmtComment); 64 | cmtCommentDao.save(cmtComment); 65 | return cmtComment; 66 | } 67 | 68 | private void controlIsCommentBlank(CmtComment cmtComment){ 69 | if(cmtComment.getComment().isBlank()){ 70 | throw new IllegalFieldException(CmtErrorMessage.COMMENT_IS_BLANK); 71 | } 72 | } 73 | 74 | public void delete(Long id){ 75 | 76 | CmtComment cmtComment = checkIfCommentExistsById(id); 77 | cmtCommentDao.delete(cmtComment); 78 | 79 | } 80 | 81 | private CmtComment checkIfCommentExistsById(Long id){ 82 | 83 | CmtComment cmtComment = cmtCommentDao.findById(id) 84 | .orElseThrow(()-> new ItemNotFoundException(CmtErrorMessage.COMMENT_NOT_FOUND)); 85 | 86 | return cmtComment; 87 | 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/dto/RestResponse.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.dto; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import java.io.Serializable; 8 | import java.util.Date; 9 | 10 | @Data 11 | @AllArgsConstructor 12 | @NoArgsConstructor 13 | public class RestResponse implements Serializable { 14 | 15 | private T data; 16 | private Date responseDate; 17 | private boolean isSuccess; 18 | private String messages; 19 | 20 | public RestResponse(T data, boolean isSuccess) { 21 | this.data = data; 22 | this.isSuccess = isSuccess; 23 | responseDate = new Date(); 24 | } 25 | 26 | public static RestResponse of(T t){ 27 | return new RestResponse<>(t, true); 28 | } 29 | 30 | public static RestResponse error(T t){ 31 | return new RestResponse<>(t, false); 32 | } 33 | 34 | public static RestResponse empty(){ 35 | return new RestResponse<>(null, true); 36 | } 37 | 38 | public void setMessages(String messages) { 39 | this.messages = messages; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/entity/BaseAdditionalFields.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.entity; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | import org.springframework.data.annotation.CreatedBy; 6 | import org.springframework.data.annotation.CreatedDate; 7 | import org.springframework.data.annotation.LastModifiedBy; 8 | import org.springframework.data.annotation.LastModifiedDate; 9 | 10 | import javax.persistence.Column; 11 | import javax.persistence.Embeddable; 12 | import java.util.Date; 13 | 14 | @Embeddable 15 | @Getter 16 | @Setter 17 | public class BaseAdditionalFields { 18 | 19 | @Column(name = "CREATE_DATE", updatable = false) 20 | @CreatedDate 21 | private Date createDate; 22 | 23 | @Column(name = "UPDATE_DATE") 24 | @LastModifiedDate 25 | private Date updateDate; 26 | 27 | @Column(name = "CREATED_BY") 28 | @CreatedBy 29 | private Long createdBy; 30 | 31 | @Column(name = "UPDATED_BY") 32 | @LastModifiedBy 33 | private Long updatedBy; 34 | } 35 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/entity/BaseEntity.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.entity; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | import javax.persistence.Embedded; 7 | import javax.persistence.MappedSuperclass; 8 | import java.io.Serializable; 9 | 10 | @MappedSuperclass 11 | @Getter 12 | @Setter 13 | public abstract class BaseEntity implements BaseModel, Cloneable, Serializable { 14 | 15 | private static final long serialVersionUID = 1L; 16 | 17 | @Embedded 18 | private BaseAdditionalFields baseAdditionalFields; 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/entity/BaseModel.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.entity; 2 | 3 | public interface BaseModel { 4 | Long getId(); 5 | } 6 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/enums/BaseErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.enums; 2 | 3 | public interface BaseErrorMessage { 4 | String getMessage(); 5 | } 6 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/enums/GenErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.enums; 2 | 3 | public enum GenErrorMessage implements BaseErrorMessage{ 4 | 5 | ITEM_NOT_FOUND("Item not found!"), 6 | ; 7 | 8 | private String message; 9 | GenErrorMessage(String message) { 10 | this.message = message; 11 | } 12 | 13 | public String getMessage() { 14 | return message; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return message; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/exception/GenCustomizedResponseEntityExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.exception; 2 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 3 | import org.springframework.http.HttpHeaders; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.http.ResponseEntity; 6 | import org.springframework.web.bind.MethodArgumentNotValidException; 7 | import org.springframework.web.bind.annotation.ControllerAdvice; 8 | import org.springframework.web.bind.annotation.ExceptionHandler; 9 | import org.springframework.web.bind.annotation.RestController; 10 | import org.springframework.web.context.request.WebRequest; 11 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 12 | 13 | import java.util.Date; 14 | 15 | @RestController 16 | @ControllerAdvice 17 | public class GenCustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { 18 | 19 | @ExceptionHandler 20 | public final ResponseEntity handleAllExceptions(Exception ex, WebRequest webRequest){ 21 | 22 | Date errorDate = new Date(); 23 | String message = ex.getMessage(); 24 | String description = webRequest.getDescription(false); 25 | 26 | GenExceptionResponse genExceptionResponse = new GenExceptionResponse(errorDate, message, description); 27 | 28 | return new ResponseEntity<>(genExceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR); 29 | } 30 | 31 | @ExceptionHandler 32 | public final ResponseEntity handleAllItemNotFoundException(ItemNotFoundException ex, WebRequest webRequest){ 33 | 34 | Date errorDate = new Date(); 35 | String message = ex.getMessage(); 36 | String description = webRequest.getDescription(false); 37 | 38 | GenExceptionResponse genExceptionResponse = new GenExceptionResponse(errorDate, message, description); 39 | 40 | return new ResponseEntity<>(genExceptionResponse, HttpStatus.NOT_FOUND); 41 | } 42 | 43 | @Override 44 | protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { 45 | 46 | Date errorDate = new Date(); 47 | String message = "Validation failed!"; 48 | String description = ex.getBindingResult().toString(); 49 | 50 | GenExceptionResponse genExceptionResponse = new GenExceptionResponse(errorDate, message, description); 51 | 52 | return new ResponseEntity<>(genExceptionResponse, HttpStatus.BAD_REQUEST); 53 | } 54 | } -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/exception/GenExceptionResponse.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.exception; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | import java.util.Date; 7 | 8 | @Data 9 | @AllArgsConstructor 10 | public class GenExceptionResponse { 11 | 12 | private Date errorDate; 13 | private String message; 14 | private String detail; 15 | } 16 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/exceptions/IllegalFieldException.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.exceptions; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.web.bind.annotation.ResponseStatus; 6 | 7 | @ResponseStatus(HttpStatus.CONFLICT) 8 | public class IllegalFieldException extends RuntimeException{ 9 | public IllegalFieldException(BaseErrorMessage message){ 10 | super(message.getMessage()); 11 | } 12 | 13 | public IllegalFieldException(String username,String phoneNumber, BaseErrorMessage message){ 14 | super(message.getMessage()+" for username: "+username+" and phone number: "+phoneNumber); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/exceptions/ItemAlreadyExistsException.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.exceptions; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.web.bind.annotation.ResponseStatus; 6 | 7 | @ResponseStatus(HttpStatus.CONFLICT) 8 | public class ItemAlreadyExistsException extends RuntimeException{ 9 | public ItemAlreadyExistsException(BaseErrorMessage message) { 10 | super(message.getMessage()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/exceptions/ItemNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.exceptions; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.web.bind.annotation.ResponseStatus; 6 | 7 | @ResponseStatus(HttpStatus.NOT_FOUND) 8 | public class ItemNotFoundException extends RuntimeException { 9 | public ItemNotFoundException(BaseErrorMessage message) { 10 | super(message.getMessage()); 11 | } 12 | public ItemNotFoundException(String itemName,BaseErrorMessage message) { 13 | super(itemName+" named "+message.getMessage()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/exceptions/UnmatchedFieldsException.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.exceptions; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | import org.springframework.http.HttpStatus; 5 | import org.springframework.web.bind.annotation.ResponseStatus; 6 | 7 | @ResponseStatus(HttpStatus.CONFLICT) 8 | public class UnmatchedFieldsException extends RuntimeException{ 9 | public UnmatchedFieldsException(BaseErrorMessage message){super(message.getMessage());} 10 | } 11 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/service/BaseEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.service; 2 | 3 | import com.softtech.softtechspringboot.gen.entity.BaseAdditionalFields; 4 | import com.softtech.softtechspringboot.gen.entity.BaseEntity; 5 | import lombok.RequiredArgsConstructor; 6 | import org.springframework.data.jpa.repository.JpaRepository; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.Date; 10 | import java.util.List; 11 | 12 | @Service 13 | @RequiredArgsConstructor 14 | public abstract class BaseEntityService> { 15 | 16 | private final D dao; 17 | 18 | public List findAll(){ 19 | return dao.findAll(); 20 | } 21 | 22 | public E save(E entity){ 23 | setAdditionalFields(entity); 24 | return dao.save(entity); 25 | } 26 | 27 | private void setAdditionalFields(E entity) { 28 | 29 | BaseAdditionalFields baseAdditionalFields = entity.getBaseAdditionalFields(); 30 | 31 | Long currentCustomerId = getCurrentCustomerId(); 32 | 33 | if (baseAdditionalFields == null){ 34 | baseAdditionalFields = new BaseAdditionalFields(); 35 | entity.setBaseAdditionalFields(baseAdditionalFields); 36 | } 37 | 38 | if (entity.getId() == null){ 39 | baseAdditionalFields.setCreateDate(new Date()); 40 | baseAdditionalFields.setCreatedBy(currentCustomerId); 41 | } 42 | 43 | baseAdditionalFields.setUpdateDate(new Date()); 44 | baseAdditionalFields.setUpdatedBy(currentCustomerId); 45 | } 46 | 47 | public void delete(E entity){ 48 | dao.delete(entity); 49 | } 50 | 51 | 52 | //TODO: control after jwt 53 | private Long getCurrentCustomerId() { 54 | Long currentCustomer = null; 55 | return currentCustomer; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/gen/util/DateUtil.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.gen.util; 2 | 3 | import java.time.Instant; 4 | import java.time.LocalDate; 5 | import java.time.LocalDateTime; 6 | import java.time.ZoneId; 7 | import java.util.Date; 8 | 9 | public class DateUtil { 10 | 11 | public static LocalDate convertToLocalDate(Date dateToConvert) { 12 | 13 | LocalDate localDate = Instant.ofEpochMilli(dateToConvert.getTime()).atZone(ZoneId.systemDefault()).toLocalDate(); 14 | 15 | return localDate; 16 | } 17 | 18 | public static LocalDateTime convertToLocalDateTime(Date dateToConvert) { 19 | return dateToConvert.toInstant() 20 | .atZone(ZoneId.systemDefault()) 21 | .toLocalDateTime(); 22 | } 23 | 24 | public static Date convertToDate(LocalDate dateToConvert) { 25 | return java.util.Date.from(dateToConvert.atStartOfDay() 26 | .atZone(ZoneId.systemDefault()) 27 | .toInstant()); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/prd/controller/PrdProductController.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.prd.controller; 2 | 3 | import com.softtech.softtechspringboot.gen.dto.RestResponse; 4 | import com.softtech.softtechspringboot.prd.service.PrdProductService; 5 | import com.softtech.softtechspringboot.prd.dto.PrdProductDto; 6 | import io.swagger.v3.oas.annotations.Operation; 7 | import lombok.RequiredArgsConstructor; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.*; 10 | 11 | import java.math.BigDecimal; 12 | import java.util.List; 13 | 14 | @RestController 15 | @RequiredArgsConstructor 16 | @RequestMapping("/api/v1/products") 17 | public class PrdProductController { 18 | 19 | private final PrdProductService prdProductService; 20 | 21 | @Operation(summary="Get all products.") 22 | @GetMapping 23 | public ResponseEntity findAll(){ 24 | List prdProductDtoList = prdProductService.findAll(); 25 | return ResponseEntity.ok(RestResponse.of(prdProductDtoList)); 26 | } 27 | 28 | @Operation(summary="Get a product by id.") 29 | @GetMapping("/{id}") 30 | public ResponseEntity findById(@PathVariable Long id){ 31 | PrdProductDto prdProductDto = prdProductService.findById(id); 32 | return ResponseEntity.ok(RestResponse.of(prdProductDto)); 33 | } 34 | 35 | @Operation(summary="Save a product.") 36 | @PostMapping("/save") 37 | public ResponseEntity save(@RequestBody PrdProductDto prdProductDto){ 38 | PrdProductDto prdProductDtoSaved = prdProductService.save(prdProductDto); 39 | return ResponseEntity.ok(RestResponse.of(prdProductDtoSaved)); 40 | } 41 | 42 | @Operation(summary="Update the price of a product.") 43 | @PatchMapping("/update-price/{id}") 44 | public ResponseEntity updatePrice(@PathVariable Long id, @RequestParam BigDecimal price){ 45 | PrdProductDto prdProductDto = prdProductService.updatePrice(id,price); 46 | return ResponseEntity.ok(RestResponse.of(prdProductDto)); 47 | } 48 | 49 | @Operation(summary="Delete a product.") 50 | @DeleteMapping("/delete/{id}") 51 | public ResponseEntity delete(@PathVariable Long id){ 52 | prdProductService.delete(id); 53 | return ResponseEntity.ok(RestResponse.empty()); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/prd/converter/PrdProductMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.prd.converter; 2 | 3 | 4 | import com.softtech.softtechspringboot.prd.dto.PrdProductDto; 5 | import com.softtech.softtechspringboot.prd.entity.PrdProduct; 6 | import org.mapstruct.Mapper; 7 | import org.mapstruct.ReportingPolicy; 8 | import org.mapstruct.factory.Mappers; 9 | 10 | import java.util.List; 11 | 12 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 13 | public interface PrdProductMapper { 14 | PrdProductMapper INSTANCE = Mappers.getMapper(PrdProductMapper.class); 15 | 16 | PrdProduct convertToPrdProduct(PrdProductDto prdProductDto); 17 | List convertToPrdProductDtoList(List prdProductList); 18 | PrdProductDto convertToPrdProductDto(PrdProduct prdProduct); 19 | } 20 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/prd/dao/PrdProductDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.prd.dao; 2 | 3 | import com.softtech.softtechspringboot.prd.entity.PrdProduct; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface PrdProductDao extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/prd/dto/PrdProductDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.prd.dto; 2 | 3 | import lombok.Data; 4 | 5 | import javax.validation.constraints.NotNull; 6 | import java.math.BigDecimal; 7 | 8 | @Data 9 | public class PrdProductDto { 10 | 11 | @NotNull 12 | private String name; 13 | 14 | @NotNull 15 | private BigDecimal price; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/prd/entity/PrdProduct.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.prd.entity; 2 | 3 | import com.softtech.softtechspringboot.gen.entity.BaseEntity; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | 7 | import javax.persistence.*; 8 | import java.math.BigDecimal; 9 | 10 | @Entity 11 | @Table(name="PRD_PRODUCT") 12 | @Getter @Setter 13 | public class PrdProduct extends BaseEntity { 14 | 15 | @Id 16 | @SequenceGenerator(name ="PrdProduct" , sequenceName = "PRD_PRODUCT_ID_SEQ") 17 | @GeneratedValue(generator ="PrdProduct") 18 | private Long id; 19 | 20 | @Column(name = "NAME",length=100,nullable = false) 21 | private String name; 22 | 23 | @Column(name = "PRICE",precision = 15, scale=2,nullable = false) 24 | private BigDecimal price; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/prd/enums/PrdErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.prd.enums; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | 5 | public enum PrdErrorMessage implements BaseErrorMessage { 6 | 7 | PRODUCT_NOT_FOUND("Product not found!"), 8 | ; 9 | 10 | private String message; 11 | PrdErrorMessage(String message) { 12 | this.message = message; 13 | } 14 | 15 | public String getMessage() { 16 | return message; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return message; 22 | } 23 | } -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/prd/service/PrdProductService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.prd.service; 2 | 3 | 4 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 5 | import com.softtech.softtechspringboot.prd.converter.PrdProductMapper; 6 | import com.softtech.softtechspringboot.prd.dto.PrdProductDto; 7 | import com.softtech.softtechspringboot.prd.entity.PrdProduct; 8 | import com.softtech.softtechspringboot.prd.enums.PrdErrorMessage; 9 | import com.softtech.softtechspringboot.prd.service.entityservice.PrdProductEntityService; 10 | import lombok.RequiredArgsConstructor; 11 | import org.springframework.stereotype.Service; 12 | 13 | import java.math.BigDecimal; 14 | import java.util.List; 15 | 16 | @Service 17 | @RequiredArgsConstructor 18 | public class PrdProductService { 19 | 20 | private final PrdProductEntityService prdProductEntityService; 21 | 22 | public List findAll(){ 23 | List prdProductList = prdProductEntityService.findAll(); 24 | return PrdProductMapper.INSTANCE.convertToPrdProductDtoList(prdProductList); 25 | } 26 | 27 | public PrdProductDto findById(Long id){ 28 | PrdProduct prdProduct = prdProductEntityService.findById(id); 29 | return PrdProductMapper.INSTANCE.convertToPrdProductDto(prdProduct); 30 | } 31 | 32 | public PrdProductDto save(PrdProductDto prdProductDto){ 33 | PrdProduct prdProduct = PrdProductMapper.INSTANCE.convertToPrdProduct(prdProductDto); 34 | prdProduct = prdProductEntityService.save(prdProduct); 35 | return PrdProductMapper.INSTANCE.convertToPrdProductDto(prdProduct); 36 | } 37 | 38 | public PrdProductDto updatePrice(Long id, BigDecimal price) { 39 | PrdProduct prdProduct = prdProductEntityService.updatePrice(id,price); 40 | return PrdProductMapper.INSTANCE.convertToPrdProductDto(prdProduct); 41 | } 42 | 43 | public void delete(Long id){ 44 | PrdProduct prdProduct = prdProductEntityService.findById(id); 45 | prdProductEntityService.delete(prdProduct); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/prd/service/entityservice/PrdProductEntityService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.prd.service.entityservice; 2 | 3 | import com.softtech.softtechspringboot.gen.exceptions.ItemNotFoundException; 4 | import com.softtech.softtechspringboot.gen.service.BaseEntityService; 5 | import com.softtech.softtechspringboot.prd.dao.PrdProductDao; 6 | import com.softtech.softtechspringboot.prd.entity.PrdProduct; 7 | import com.softtech.softtechspringboot.prd.enums.PrdErrorMessage; 8 | import org.springframework.stereotype.Service; 9 | 10 | import java.math.BigDecimal; 11 | import java.util.Optional; 12 | 13 | @Service 14 | public class PrdProductEntityService extends BaseEntityService { 15 | 16 | private final PrdProductDao prdProductDao; 17 | 18 | public PrdProductEntityService(PrdProductDao prdProductDao){ 19 | super(prdProductDao); 20 | this.prdProductDao = prdProductDao; 21 | } 22 | 23 | public PrdProduct findById(Long id){ 24 | 25 | PrdProduct prdProduct = prdProductDao.findById(id) 26 | .orElseThrow(()-> new ItemNotFoundException(PrdErrorMessage.PRODUCT_NOT_FOUND)); 27 | 28 | return prdProduct; 29 | } 30 | 31 | public PrdProduct updatePrice(Long id, BigDecimal price) { 32 | 33 | PrdProduct prdProduct = prdProductDao.findById(id) 34 | .orElseThrow(()->new ItemNotFoundException(PrdErrorMessage.PRODUCT_NOT_FOUND)); 35 | 36 | prdProduct.setPrice(price); 37 | return prdProductDao.save(prdProduct); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/usr/controller/UsrUserController.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.usr.controller; 2 | 3 | import com.softtech.softtechspringboot.gen.dto.RestResponse; 4 | import com.softtech.softtechspringboot.usr.dto.UsrUserDto; 5 | import com.softtech.softtechspringboot.usr.dto.UsrUserSaveDto; 6 | import com.softtech.softtechspringboot.usr.dto.UsrUserUpdateDto; 7 | import com.softtech.softtechspringboot.usr.service.UsrUserService; 8 | import io.swagger.v3.oas.annotations.Operation; 9 | import lombok.RequiredArgsConstructor; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.web.bind.annotation.*; 12 | 13 | 14 | import java.util.List; 15 | 16 | @RestController 17 | @RequiredArgsConstructor 18 | @RequestMapping("/api/v1/users") 19 | public class UsrUserController { 20 | 21 | private final UsrUserService usrUserService; 22 | 23 | @Operation(summary="Get all users.") 24 | @GetMapping 25 | public ResponseEntity findAll(){ 26 | 27 | List usrUserDtoList = usrUserService.findAll(); 28 | return ResponseEntity.ok(RestResponse.of(usrUserDtoList)); 29 | 30 | } 31 | 32 | @Operation(summary="Get a user by id.") 33 | @GetMapping("/id/{id}") 34 | public ResponseEntity findById(@PathVariable Long id){ 35 | 36 | UsrUserDto usrUserDto = usrUserService.findById(id); 37 | return ResponseEntity.ok(RestResponse.of(usrUserDto)); 38 | 39 | } 40 | 41 | @Operation(summary="Get a user by unique username.") 42 | @GetMapping("/username/{username}") 43 | public ResponseEntity findByUsername(@PathVariable String username){ 44 | 45 | UsrUserDto usrUserDto = usrUserService.findByUsername(username); 46 | return ResponseEntity.ok(RestResponse.of(usrUserDto)); 47 | 48 | } 49 | 50 | @Operation(summary="Save a user.") 51 | @PostMapping("/save") 52 | public ResponseEntity save(@RequestBody UsrUserSaveDto usrUserSaveDto){ 53 | 54 | UsrUserDto usrUserDtoSaved = usrUserService.save(usrUserSaveDto); 55 | return ResponseEntity.ok(RestResponse.of(usrUserDtoSaved)); 56 | 57 | } 58 | 59 | @Operation(summary="Update a user.") 60 | @PatchMapping("/update") 61 | public ResponseEntity update(@RequestBody UsrUserUpdateDto usrUserUpdateDto){ 62 | 63 | UsrUserDto usrUserDtoUpdated = usrUserService.update(usrUserUpdateDto); 64 | return ResponseEntity.ok(RestResponse.of(usrUserDtoUpdated)); 65 | 66 | } 67 | 68 | @Operation(summary="Delete a user.") 69 | @DeleteMapping("/delete") 70 | public ResponseEntity delete(@RequestParam String username, String phoneNumber ) { 71 | usrUserService.delete(username,phoneNumber); 72 | return ResponseEntity.ok(RestResponse.empty()); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/usr/converter/UsrUserMapper.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.usr.converter; 2 | 3 | 4 | import com.softtech.softtechspringboot.usr.dto.UsrUserDto; 5 | import com.softtech.softtechspringboot.usr.dto.UsrUserSaveDto; 6 | import com.softtech.softtechspringboot.usr.dto.UsrUserUpdateDto; 7 | import com.softtech.softtechspringboot.usr.entity.UsrUser; 8 | import org.mapstruct.Mapper; 9 | import org.mapstruct.ReportingPolicy; 10 | import org.mapstruct.factory.Mappers; 11 | 12 | import java.util.List; 13 | 14 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 15 | public interface UsrUserMapper { 16 | 17 | UsrUserMapper INSTANCE = Mappers.getMapper(UsrUserMapper.class); 18 | 19 | UsrUser convertToUsrUser(UsrUserDto usrUserDto); 20 | UsrUser convertToUsrUser(UsrUserSaveDto usrUserSaveDto); 21 | UsrUser convertToUsrUser(UsrUserUpdateDto usrUserUpdateDto); 22 | 23 | List convertToUsrUserDtoList(List usrUserList); 24 | UsrUserDto convertToUsrUserDto(UsrUser usrUser); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/usr/dao/UsrUserDao.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.usr.dao; 2 | 3 | import com.softtech.softtechspringboot.usr.entity.UsrUser; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | import java.util.Optional; 8 | 9 | @Repository 10 | public interface UsrUserDao extends JpaRepository { 11 | 12 | Optional findByUsername(String username); 13 | Optional findByEmail(String email); 14 | Optional findByPhoneNumber(String phoneNumber); 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/usr/dto/UsrUserDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.usr.dto; 2 | 3 | import com.softtech.softtechspringboot.usr.enums.UsrUserType; 4 | import lombok.Data; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | 9 | @Data 10 | public class UsrUserDto { 11 | 12 | @NotNull 13 | private String username; 14 | 15 | @NotNull 16 | private String email; 17 | 18 | @NotNull 19 | private String phoneNumber; 20 | 21 | @NotNull 22 | private UsrUserType userType; 23 | } 24 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/usr/dto/UsrUserSaveDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.usr.dto; 2 | 3 | import com.softtech.softtechspringboot.usr.enums.UsrUserType; 4 | import lombok.Data; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | @Data 9 | public class UsrUserSaveDto { 10 | 11 | @NotNull 12 | private String username; 13 | 14 | @NotNull 15 | private String email; 16 | 17 | @NotNull 18 | private String phoneNumber; 19 | 20 | @NotNull 21 | private UsrUserType userType; 22 | } 23 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/usr/dto/UsrUserUpdateDto.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.usr.dto; 2 | 3 | import com.softtech.softtechspringboot.usr.enums.UsrUserType; 4 | import lombok.Data; 5 | 6 | import javax.validation.constraints.NotNull; 7 | 8 | @Data 9 | public class UsrUserUpdateDto { 10 | 11 | private Long id; 12 | 13 | @NotNull 14 | private String username; 15 | 16 | @NotNull 17 | private String email; 18 | 19 | @NotNull 20 | private String phoneNumber; 21 | 22 | @NotNull 23 | private UsrUserType userType; 24 | } 25 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/usr/entity/UsrUser.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.usr.entity; 2 | 3 | import com.softtech.softtechspringboot.gen.entity.BaseEntity; 4 | import com.softtech.softtechspringboot.usr.enums.UsrUserType; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | 8 | import javax.persistence.*; 9 | 10 | @Entity 11 | @Table(name="USR_USER") 12 | @Getter 13 | @Setter 14 | public class UsrUser extends BaseEntity { 15 | 16 | @Id 17 | @SequenceGenerator(name ="UsrUser" ,sequenceName = "USR_USER_ID_SEQ") 18 | @GeneratedValue(generator = "UsrUser") 19 | private Long id; 20 | 21 | @Column(name="USERNAME",length=30,unique=true,nullable = false) 22 | private String username; 23 | 24 | @Column(name="EMAIL",length=30,unique = true,nullable = false) 25 | private String email; 26 | 27 | @Column(name="PHONE_NUMBER",length=15,unique=true,nullable = false) 28 | private String phoneNumber; 29 | 30 | @Enumerated(EnumType.STRING) 31 | @Column(name="USER_TYPE",nullable = false) 32 | private UsrUserType userType; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/usr/enums/UsrErrorMessage.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.usr.enums; 2 | 3 | import com.softtech.softtechspringboot.gen.enums.BaseErrorMessage; 4 | 5 | public enum UsrErrorMessage implements BaseErrorMessage { 6 | 7 | USER_NOT_FOUND("User not found!"), 8 | USERNAME_ALREADY_EXISTS("Username of the user already exists!"), 9 | EMAIL_ALREADY_EXISTS("Email of the user already exists!"), 10 | PHONE_NUMBER_ALREADY_EXISTS("Phone number of the user already exists!"), 11 | UNMATCHED_USERNAME_AND_PHONE_NUMBER("Username and phone number do not match") 12 | ; 13 | 14 | private String message; 15 | UsrErrorMessage(String message) { 16 | this.message = message; 17 | } 18 | 19 | public String getMessage() { 20 | return message; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return message; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/usr/enums/UsrUserType.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.usr.enums; 2 | 3 | public enum UsrUserType { 4 | PERSON, 5 | INSTITUTION 6 | } 7 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/java/com/softtech/softtechspringboot/usr/service/UsrUserService.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot.usr.service; 2 | 3 | import com.softtech.softtechspringboot.usr.converter.UsrUserMapper; 4 | import com.softtech.softtechspringboot.usr.dto.UsrUserDto; 5 | import com.softtech.softtechspringboot.usr.dto.UsrUserSaveDto; 6 | import com.softtech.softtechspringboot.usr.dto.UsrUserUpdateDto; 7 | import com.softtech.softtechspringboot.usr.entity.UsrUser; 8 | import com.softtech.softtechspringboot.usr.service.entityservice.UsrUserEntityService; 9 | import lombok.RequiredArgsConstructor; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.List; 13 | 14 | @Service 15 | @RequiredArgsConstructor 16 | public class UsrUserService { 17 | 18 | private final UsrUserEntityService usrUserEntityService; 19 | 20 | public List findAll() { 21 | List usrUserList = usrUserEntityService.findAll(); 22 | return UsrUserMapper.INSTANCE.convertToUsrUserDtoList(usrUserList); 23 | } 24 | public UsrUserDto findById(Long id){ 25 | UsrUser usrUser = usrUserEntityService.findByIdWithControl(id); 26 | return UsrUserMapper.INSTANCE.convertToUsrUserDto(usrUser); 27 | } 28 | public UsrUserDto findByUsername(String username){ 29 | UsrUser usrUser = usrUserEntityService.getByUsernameWithControl(username); 30 | return UsrUserMapper.INSTANCE.convertToUsrUserDto(usrUser); 31 | } 32 | 33 | public UsrUserDto save(UsrUserSaveDto usrUserSaveDto) { 34 | UsrUser usrUser = UsrUserMapper.INSTANCE.convertToUsrUser(usrUserSaveDto); 35 | usrUser = usrUserEntityService.saveWithControl(usrUser); 36 | return UsrUserMapper.INSTANCE.convertToUsrUserDto(usrUser); 37 | } 38 | 39 | public UsrUserDto update(UsrUserUpdateDto usrUserUpdateDto) { 40 | UsrUser usrUser = UsrUserMapper.INSTANCE.convertToUsrUser(usrUserUpdateDto); 41 | usrUser = usrUserEntityService.update(usrUser); 42 | return UsrUserMapper.INSTANCE.convertToUsrUserDto(usrUser); 43 | } 44 | 45 | public void delete(String username, String phoneNumber) { 46 | usrUserEntityService.deleteWithControl(username,phoneNumber); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:testdb 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password=password 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | 7 | application.title = case-2 8 | application.version = 1.0.0 9 | 10 | server.port = 8080 11 | 12 | logging.level.org.springframework = info 13 | 14 | spring.jpa.show-sql = true 15 | spring.jpa.hibernate.ddl-auto=create 16 | 17 | -------------------------------------------------------------------------------- /Projects/Case3/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | _______ _____ _______ _______ _______ _______ _______ _ _ 2 | |______ | | |______ | | |______ | |_____| 3 | ______| |_____| | | | |______ |_____ | | 4 | 5 | ${application.title} ${application.version} 6 | Powered by Spring Boot ${spring-boot.version} -------------------------------------------------------------------------------- /Projects/Case3/src/test/java/com/softtech/softtechspringboot/SofttechSpringBootApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.softtech.softtechspringboot; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SofttechSpringBootApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring 2 | The Spring Projects and documentations that I made for learning the Spring Framework. 3 | --------------------------------------------------------------------------------