Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • R2dbc Rest Api relation problem

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 267
    Answer it

    Hi everyone,

    I am write simple project in Webflux Rest api not problem the response.

    My problem is that the code is working, but the response is wrong, so for example the label has 3 but 6 returns, the projects are repeated, I don't understand what the problem is.

    This is model layer 
        
    Due class

    @Getter
        @Setter
        @AllArgsConstructor
        @Builder
        public class Due implements Serializable {
    
          @Id
          private Integer id;
          private Date my_data;
          private Boolean recurring;
          private String my_string;
    
         }

     

    Label class           

    @Getter
         @Setter
         @NoArgsConstructor
         @AllArgsConstructor
         @Builder
         public class Label {
    
           @Id
           @Column("label_id")
           private Integer id;
           private String name;
           private Integer color;
           @Column("order_data")
           private Integer orderdata;
           private Boolean favorite;
           @JsonIgnore
           private List<Task> tasks = new ArrayList<>();
    
         }

     

    Project class             

    @Getter
          @Setter
          @NoArgsConstructor
          @AllArgsConstructor
          @Builder
          public class Project {
    
              @Id
              @Column("project_id")
              private Integer id;
              private String name;
              private Integer comment_count;
              @Column("orderdata")
              private Integer order;
              private Integer color;
              private Boolean shared;
              private Boolean favorite;
              private Boolean team_inbox;
              private Boolean inbox_project;
              private String url;
              private Integer task_id;
    
         }

     

    Task class        

    @Getter
            @Setter
            @Builder
            @NoArgsConstructor
            @AllArgsConstructor
            public class Task implements Serializable {
    
              @Id
              private Integer id;
              private Integer comment_count;
              private Boolean completed;
              private String content;
              private String url;
              private Integer orderdata;
              private Integer priority;
              private Integer due_id;
              private List<Project> project;
              private List<Label> labels;
    
          }
    
    

     

    My Mapping TaskDto class                 

    @Getter
             @Setter
             @Builder
             public class TaskDto implements Serializable {
    
                 private Integer id;
                 private Integer comment_count;
                 private Boolean completed;
                 private String content;
                 private String url;
                 private Integer orderdata;
                 private Integer priority;
                 private Due due;
                 private List<Project> project;
                 private List<Label> labels;
    
             }

     

    My TaskManager

    @Component
        @RequiredArgsConstructor
        public class TaskManager {
    
      private final TasksRepository tasksRepository;
      private final DueRepository dueRepository;
      private final ProjectRepository projectRepository;
      private final LabelRepository labelRepository;
    
      public Flux<TaskDto> findAll() {
    
        List<Project> projectList = new ArrayList<>();
        List<Label> label_ids = new ArrayList<>();
    
        Flux<Task> tasks = tasksRepository.findAll();
        Flux<Due> allTasks = tasks.flatMap(task -> dueRepository.findById(task.getDue_id()));
    
        Due.DueBuilder newDue = Due.builder();
        Project.ProjectBuilder newProject = Project.builder();
        Label.LabelBuilder newLabel = Label.builder();
        TaskDto.TaskDtoBuilder newTask = TaskDto.builder();
    
        return allTasks.map(due -> newDue
            .recurring(due.getRecurring())
            .my_data(due.getMy_data())
            .my_string(due.getMy_string())
            .id(due.getId()))
            .thenMany(tasks)
            .flatMap(p -> projectRepository.findAll()
                .map(t -> newProject.id(t.getId())
                    .color(t.getColor())
                    .comment_count(t.getComment_count())
                    .inbox_project(t.getInbox_project())
                    .order(t.getOrder())
                    .favorite(t.getFavorite())
                    .name(t.getName())
                    .shared(t.getShared())
                    .team_inbox(t.getTeam_inbox())
                    .task_id(t.getTask_id())
                    .url(t.getUrl()).build())
                .map(projectList::add)
                .thenMany(tasks)
                .flatMap(l -> labelRepository.findAll())
                .map(label -> newLabel.id(label.getId())
                    .color(label.getColor())
                    .name(label.getName())
                    .favorite(label.getFavorite())
                    .orderdata(label.getOrderdata())
                    .build())
                .map(label_ids::add)
                .thenMany(tasks)
                .map(task -> newTask
                    .id(task.getId())
                    .url(task.getUrl())
                    .orderdata(task.getOrderdata())
                    .comment_count(task.getComment_count())
                    .completed(task.getCompleted())
                    .content(task.getContent())
                    .priority(task.getPriority())
                    .labels(label_ids)
                    .due(newDue.build())
                    .project(projectList).build())
            ).doOnError(System.out::println);
         }
    
      }

     

    Response Json

     {
      "id": 1,
      "comment_count": 10,
      "completed": false,
      "content": "new conent",
      "url": "http.google.com",
      "orderdata": 5,
      "priority": 5,
      "due": {
       "id": 2,
       "my_data": "2021-03-23",
       "recurring": true,
       "my_string": "new string my"
      },
      "project": [
       {
        "id": 1,
        "name": "new name",
        "comment_count": 9,
        "order": 4,
        "color": 4,
        "shared": false,
        "favorite": true,
        "team_inbox": false,
        "inbox_project": true,
        "url": "http://google.com",
        "task_id": 1
       },
       {
        "id": 2,
        "name": "new name2",
        "comment_count": 6,
        "order": 3,
        "color": 3,
        "shared": false,
        "favorite": false,
        "team_inbox": false,
        "inbox_project": false,
        "url": "http://youtube.com",
        "task_id": 1
       },
       {
        "id": 3,
        "name": "new project name3",
        "comment_count": 4,
        "order": 5,
        "color": 3,
        "shared": true,
        "favorite": false,
        "team_inbox": false,
        "inbox_project": false,
        "url": "https://bbcnews.com",
        "task_id": 2
       },
       {
        "id": 1,
        "name": "new name",
        "comment_count": 9,
        "order": 4,
        "color": 4,
        "shared": false,
        "favorite": true,
        "team_inbox": false,
        "inbox_project": true,
        "url": "http://google.com",
        "task_id": 1
       },
       {
        "id": 2,
        "name": "new name2",
        "comment_count": 6,
        "order": 3,
        "color": 3,
        "shared": false,
        "favorite": false,
        "team_inbox": false,
        "inbox_project": false,
        "url": "http://youtube.com",
        "task_id": 1
       },
       {
        "id": 3,
        "name": "new project name3",
        "comment_count": 4,
        "order": 5,
        "color": 3,
        "shared": true,
        "favorite": false,
        "team_inbox": false,
        "inbox_project": false,
        "url": "https://bbcnews.com",
        "task_id": 2
       }
      ],
      "labels": [
       {
        "id": 1,
        "name": "new name label",
        "color": 5,
        "orderdata": 5,
        "favorite": false
       },
       {
        "id": 2,
        "name": "new name label2",
        "color": 3,
        "orderdata": 4,
        "favorite": true
       },
       {
        "id": 3,
        "name": "new name label3",
        "color": 5,
        "orderdata": 6,
        "favorite": false
       },
       {
        "id": 1,
        "name": "new name label",
        "color": 5,
        "orderdata": 5,
        "favorite": false
       },
       {
        "id": 2,
        "name": "new name label2",
        "color": 3,
        "orderdata": 4,
        "favorite": true
       },
       {
        "id": 3,
        "name": "new name label3",
        "color": 5,
        "orderdata": 6,
        "favorite": false
       },
       {
        "id": 1,
        "name": "new name label",
        "color": 5,
        "orderdata": 5,
        "favorite": false
       },
       {
        "id": 2,
        "name": "new name label2",
        "color": 3,
        "orderdata": 4,
        "favorite": true
       },
       {
        "id": 3,
        "name": "new name label3",
        "color": 5,
        "orderdata": 6,
        "favorite": false
       },
       {
        "id": 1,
        "name": "new name label",
        "color": 5,
        "orderdata": 5,
        "favorite": false
       },
       {
        "id": 2,
        "name": "new name label2",
        "color": 3,
        "orderdata": 4,
        "favorite": true
       },
       {
        "id": 3,
        "name": "new name label3",
        "color": 5,
        "orderdata": 6,
        "favorite": false
       }
     
      }

     

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: