Rental Application (React & Spring boot Microservice) - 15 : rental-service(1)

16977 ワード

#1 rental-service


relater-serviceはpost-serviceが借用を要求したときに借用を生成するクラスである.このほか、リースリスト照会、リース詳細照会などの機能も実行できます.
大まかなシーンを見てみましょう.
1)借用の作成
1−1)投稿者としてのユーザ−aと,貸し出しを希望するユーザ−bとが協調し,貸し出しの準備をする.
1−2)ユーザ−bは、投稿上の「借用」ボタンを押して借用し、借用に関する受入要求をユーザ−aに送信する.
1−3)ユーザ−aの承認が完了すると、借用が生成される.
2)リースリストの表示
2-1)ユーザー-aは私のページに入り、借りたものを表示します.
2)Myページの「借用リスト」タブにアクセスし、借用項目を表示します.
3)デビットリストの表示
3-1)プレイヤー-bは私のページに入り、借りた借り物を表示します.
3-2)Mypageの「デビット・シート」タブに入り、デビット・シートを表示します.
2、3共通)詳細項目照会
1)[リスト]タブで、[プロジェクト]をクリックして詳細を表示します.
以上のシナリオに対して、リース・サービスを実施します.

#2プロジェクトの作成



  • pom.xml
  • <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
  • RentalController
  • package com.microservices.rentalservice.controller;
    
    import com.microservices.rentalservice.dto.RentalDto;
    import com.microservices.rentalservice.service.RentalService;
    import com.microservices.rentalservice.vo.RequestCreate;
    import com.microservices.rentalservice.vo.ResponseRental;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.env.Environment;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @RestController
    @Slf4j
    @RequestMapping("/")
    public class RentalController {
        private RentalService rentalService;
        private Environment env;
    
        @Autowired
        public RentalController(
            RentalService rentalService,
            Environment env
        ) {
            this.rentalService = rentalService;
            this.env = env;
        }
    
        @GetMapping("/health_check")
        public String status() {
            return String.format(
                "It's working in Post Service"
                    + ", port(local.server.port) =" + env.getProperty("local.server.port")
                    + ", port(server.port) =" + env.getProperty("server.port")
            );
        }
    
        @PostMapping("/create")
        public ResponseEntity<?> createRental(@RequestBody RequestCreate rentalVo) {
            log.info("Rental Service's Controller Layer :: Call createRental Method!");
    
            RentalDto rentalDto = RentalDto.builder()
                                           .postId(rentalVo.getPostId())
                                           .price(rentalVo.getPrice())
                                           .owner(rentalVo.getOwner())
                                           .borrower(rentalVo.getBorrower())
                                           .startDate(rentalVo.getStartDate())
                                           .endDate(rentalVo.getEndDate())
                                           .build();
    
            RentalDto rental = rentalService.createRental(rentalDto);
            ResponseRental responseRental = ResponseRental.builder()
                                                          .rentalId(rental.getRentalId())
                                                          .postId(rentalVo.getPostId())
                                                          .price(rentalVo.getPrice())
                                                          .owner(rentalVo.getOwner())
                                                          .borrower(rentalVo.getBorrower())
                                                          .startDate(rentalVo.getStartDate())
                                                          .endDate(rentalVo.getEndDate())
                                                          .createdAt(rental.getCreatedAt())
                                                          .build();
    
            return ResponseEntity.status(HttpStatus.CREATED).body(responseRental);
        }
    
        @GetMapping("/{rentalId}/rental")
        public ResponseEntity<?> getRentalByRentalId(@PathVariable("rentalId") String rentalId) {
            log.info("Rental Service's Controller Layer :: Call getRentalByRentalId Method!");
    
            RentalDto rentalDto = rentalService.getRentalByRentalId(rentalId);
    
            return ResponseEntity.status(HttpStatus.OK).body(ResponseRental.builder()
                                                                           .rentalId(rentalDto.getRentalId())
                                                                           .postId(rentalDto.getPostId())
                                                                           .price(rentalDto.getPrice())
                                                                           .owner(rentalDto.getOwner())
                                                                           .borrower(rentalDto.getBorrower())
                                                                           .startDate(rentalDto.getStartDate())
                                                                           .endDate(rentalDto.getEndDate())
                                                                           .createdAt(rentalDto.getCreatedAt())
                                                                           .build());
        }
    
        @GetMapping("/{owner}/my_rentals")
        public ResponseEntity<?> getRentalsByOwner(@PathVariable("owner") String owner) {
            log.info("Rental Service's Controller Layer :: Call getMyRentalByUserId Method!");
    
            Iterable<RentalDto> rentalList = rentalService.getRentalsByOwner(owner);
            List<ResponseRental> result = new ArrayList<>();
    
            rentalList.forEach(v -> {
                result.add(ResponseRental.builder()
                                         .rentalId(v.getRentalId())
                                         .postId(v.getPostId())
                                         .price(v.getPrice())
                                         .owner(v.getOwner())
                                         .borrower(v.getBorrower())
                                         .startDate(v.getStartDate())
                                         .endDate(v.getEndDate())
                                         .createdAt(v.getCreatedAt())
                                         .build());
            });
    
            return ResponseEntity.status(HttpStatus.OK).body(result);
        }
    
        @GetMapping("/{borrower}/borrow_rentals")
        public ResponseEntity<?> getRentalsByBorrower(@PathVariable("borrower") String borrower) {
            log.info("Rental Service's Controller Layer :: Call getBorrowRentalByUserId Method!");
    
            Iterable<RentalDto> rentalList = rentalService.getRentalsByBorrower(borrower);
            List<ResponseRental> result = new ArrayList<>();
    
            rentalList.forEach(v -> {
                result.add(ResponseRental.builder()
                                         .rentalId(v.getRentalId())
                                         .postId(v.getPostId())
                                         .price(v.getPrice())
                                         .owner(v.getOwner())
                                         .borrower(v.getBorrower())
                                         .startDate(v.getStartDate())
                                         .endDate(v.getEndDate())
                                         .createdAt(v.getCreatedAt())
                                         .build());
            });
    
            return ResponseEntity.status(HttpStatus.OK).body(result);
        }
    
        @DeleteMapping("/{rentalId}/cancel")
        public ResponseEntity<?> deleteRental(@PathVariable("rentalId") String rentalId) {
            log.info("Rental Service's Controller Layer :: Call deleteRental Method!");
    
            RentalDto rentalDto = rentalService.deleteRental(rentalId);
    
            ResponseRental responseRental = ResponseRental.builder()
                .rentalId(rentalDto.getRentalId())
                .build();
    
            return ResponseEntity.status(HttpStatus.OK).body(responseRental.getRentalId() + " :: Successfully delete");
        }
    }
    コントローラセクションを作成した後、エラーコードの修正を続けます.次にvo、dto、entityクラスを作成します
  • RentalEntity
  • package com.microservices.rentalservice.entity;
    
    import lombok.Builder;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import javax.persistence.*;
    
    @Data
    @Entity
    @Table(name="rentals")
    @NoArgsConstructor
    public class RentalEntity {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        @Column(nullable = false)
        private String rentalId;
    
        @Column(nullable = false)
        private Long postId;
    
        @Column(nullable = false)
        private Long price;
    
        @Column(nullable = false)
        private String owner;
    
        @Column(nullable = false)
        private String borrower;
    
        @Column(nullable = false)
        private String startDate;
    
        @Column(nullable = false)
        private String endDate;
    
        @Column(nullable = false)
        private String createdAt;
    
        @Builder
        public RentalEntity(
            String rentalId,
            Long postId,
            Long price,
            String owner,
            String borrower,
            String startDate,
            String endDate,
            String createdAt
        ) {
            this.rentalId = rentalId;
            this.postId = postId;
            this.price = price;
            this.owner = owner;
            this.borrower = borrower;
            this.startDate = startDate;
            this.endDate = endDate;
            this.createdAt = createdAt;
        }
    }
  • vo.RequestCreate
  • package com.microservices.rentalservice.vo;
    
    import lombok.Getter;
    
    import javax.validation.constraints.NotNull;
    
    @Getter
    public class RequestCreate {
        @NotNull(message="PostId cannot be null")
        private Long postId;
    
        @NotNull(message="Price cannot be null")
        private Long price;
    
        @NotNull(message="Owner cannot be null")
        private String owner;
    
        @NotNull(message="Borrower cannot be null")
        private String borrower;
    
        @NotNull(message="StartDate cannot be null")
        private String startDate;
    
        @NotNull(message="EndDate cannot be null")
        private String endDate;
    }
  • ResponseRental
  • package com.microservices.rentalservice.vo;
    
    import com.fasterxml.jackson.annotation.JsonInclude;
    import lombok.Builder;
    import lombok.Data;
    
    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class ResponseRental {
        private String rentalId;
        private Long postId;
        private Long price;
        private String owner;
        private String borrower;
        private String startDate;
        private String endDate;
        private String createdAt;
    
        @Builder
        public ResponseRental(
            String rentalId,
            Long postId,
            Long price,
            String owner,
            String borrower,
            String startDate,
            String endDate,
            String createdAt
        ) {
            this.rentalId = rentalId;
            this.postId = postId;
            this.price = price;
            this.owner = owner;
            this.borrower = borrower;
            this.startDate = startDate;
            this.endDate = endDate;
            this.createdAt = createdAt;
        }
    }
  • RentalDto
  • package com.microservices.rentalservice.dto;
    
    import com.fasterxml.jackson.annotation.JsonInclude;
    import lombok.Builder;
    import lombok.Data;
    
    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class RentalDto {
        private String rentalId;
        private Long postId;
        private Long price;
        private String owner;
        private String borrower;
        private String startDate;
        private String endDate;
        private String createdAt;
    
        @Builder
        public RentalDto(
            String rentalId,
            Long postId,
            Long price,
            String owner,
            String borrower,
            String startDate,
            String endDate,
            String createdAt
        ) {
            this.rentalId = rentalId;
            this.postId = postId;
            this.price = price;
            this.owner = owner;
            this.borrower = borrower;
            this.startDate = startDate;
            this.endDate = endDate;
            this.createdAt = createdAt;
        }
    }
    コントローラからdtoクラスへの実装が完了したら、次の記事でサービス、リポジトリ、カフカについて説明します.