how to return data spring boot

71 Views Asked by At

I create Rest API in spring boot

public class Garage {
    private Integer id;
    private String address;
    private Integer numberOfSpaces;
    private boolean accessLpg;
    private double width;

also I have

public class GarageDto {
  private Integer id;
    private String address;
    private Integer numberOfSpaces;
    private boolean accessLpg;
    private double width;

public static GarageDto fromEntity(Garage garage) {
//INSIDE METHOD TO MAPPING FROM ENTITY TO DTO 
  }

I need return additional information in Garage like FreeSpots and BusySpots (Already have also car entity)

How to add this information ? I need other class or can I add this to GarageDTO ? or I need Mapper ?

Can someone help me ?

I tried something like this

public class GarageDto {

    private Integer id;
    private String address;
    private Integer numberOfSpaces;
    private boolean accessLpg;
    private double width;
    private int freeSpaces;
    private int busySpaces;



    public static GarageDto fromEntity(Garage garage) {
        GarageDtoBuilder builder = GarageDto.builder()
                .id(garage.getId())
                .address(garage.getAddress())
                .numberOfSpaces(garage.getNumberOfSpaces())
                .accessLpg(garage.isAccessLpg())
                .width(garage.getWidth());

        if (garage.getCars() != null) {
            builder.freeSpaces(garage.getNumberOfSpaces() - garage.getCars().size())
                    .busySpaces(garage.getCars().size());
        } else {
            builder.freeSpaces(garage.getNumberOfSpaces())
                    .busySpaces(0);
        }

        return builder.build();
    }

}

but I thinking its not good solution

1

There are 1 best solutions below

0
Anton Kolesov On

I think Mapstruct or similar mapping libraries can help you.

In Mapstruct see defaultExpression in @Mapping or @AfterMapping.

GarageMapper.java:

package com.example.demo.mapper;

import com.example.demo.dto.GarageDto;
import com.example.demo.entity.Garage;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public abstract class GarageMapper {
    public abstract GarageDto toDto(Garage garage);


    @AfterMapping
    public void toDto(Garage garage,  @MappingTarget GarageDto garageDto){
        garageDto.setBusySpaces(garage.getCars().size());
        garageDto.setFreeSpaces(garage.getNumberOfSpaces() - garage.getCars().size());
    }
}

ExampleService.java:

@Service
public class ExampleService {

    @Autowired
    private GarageMapper garageMapper;

    @PostConstruct
    public void example() {
        Garage garage = new Garage();
        garage.setId(10);
        garage.setWidth(20.0);
        garage.setAddress("Example address");
        garage.setAccessLpg(true);
        garage.setNumberOfSpaces(5);
        garage.setCars(List.of(new Car(), new Car()));

        System.out.println(garage);

        var dto = garageMapper.toDto(garage);
        System.out.println(dto);
    }
}

Output:

Garage(id=10, address=Example address, numberOfSpaces=5, accessLpg=true, width=20.0, cars=[com.example.demo.entity.Car@4f9b17e1, com.example.demo.entity.Car@7174145c])
GarageDto(id=10, address=Example address, numberOfSpaces=5, accessLpg=true, width=20.0, freeSpaces=3, busySpaces=2)