Skip to content

Commit

Permalink
Merge pull request #165 from afet-yonetim-sistemi/bug/164/fix-assignm…
Browse files Browse the repository at this point in the history
…ent-detail-response

#164 | Updated the Assignment Detail Service Response
  • Loading branch information
AhmetAksunger authored Sep 7, 2023
2 parents 63f984b + c8ad375 commit af444a2
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.ays.assignment.model.dto.response;

import com.ays.assignment.model.enums.AssignmentStatus;
import com.ays.common.model.AysPhoneNumber;
import com.ays.common.model.dto.response.BaseResponse;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

/**
Expand All @@ -15,9 +19,32 @@ public class AssignmentResponse extends BaseResponse {

private String id;
private String description;
private AssignmentStatus status;
private String firstName;
private String lastName;
private Double longitude;
private Double latitude;
private AysPhoneNumber phoneNumber;
private Location location;
private User user;

@Getter
@Setter
@Builder
public static class User {

private String id;
private String firstName;
private String lastName;
private AysPhoneNumber phoneNumber;
private Location location;

}

@Getter
@Builder
public static class Location {
private Double longitude;
private Double latitude;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
public interface AssignmentEntityToAssignmentMapper extends BaseMapper<AssignmentEntity, Assignment> {

@Override
@Mapping(target = "phoneNumber.countryCode", source = "source.countryCode")
@Mapping(target = "phoneNumber.lineNumber", source = "source.lineNumber")
@Mapping(target = "phoneNumber.countryCode", source = "countryCode")
@Mapping(target = "phoneNumber.lineNumber", source = "lineNumber")
@Mapping(target = "user.phoneNumber.countryCode", source = "user.countryCode")
@Mapping(target = "user.phoneNumber.lineNumber", source = "user.lineNumber")
Assignment map(AssignmentEntity source);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
public interface AssignmentToAssignmentResponseMapper extends BaseMapper<Assignment, AssignmentResponse> {

@Override
@Mapping(target = "longitude", source = "source.point.x")
@Mapping(target = "latitude", source = "source.point.y")
@Mapping(target = "location.longitude", source = "point.x")
@Mapping(target = "location.latitude", source = "point.y")
AssignmentResponse map(Assignment source);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
import com.ays.assignment.service.AssignmentService;
import com.ays.assignment.util.exception.AysAssignmentNotExistByIdException;
import com.ays.auth.model.AysIdentity;
import com.ays.location.model.UserLocation;
import com.ays.location.model.entity.UserLocationEntity;
import com.ays.location.model.mapper.UserLocationEntityToUserLocationMapper;
import com.ays.location.repository.UserLocationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@RequiredArgsConstructor
class AssignmentServiceImpl implements AssignmentService {

private final AssignmentRepository assignmentRepository;
private final UserLocationRepository userLocationRepository;

private final AysIdentity identity;

private static final AssignmentEntityToAssignmentMapper assignmentEntityToAssignmentMapper = AssignmentEntityToAssignmentMapper.initialize();
private static final UserLocationEntityToUserLocationMapper userLocationEntityToUserLocationMapper = UserLocationEntityToUserLocationMapper.initialize();

/**
* Retrieves an assignment by their ID.
Expand All @@ -33,6 +41,20 @@ public Assignment getAssignmentById(String id) {
final AssignmentEntity assignmentEntity = assignmentRepository.findByIdAndInstitutionId(id, identity.getInstitutionId())
.orElseThrow(() -> new AysAssignmentNotExistByIdException(id));

return assignmentEntityToAssignmentMapper.map(assignmentEntity);
Assignment assignment = assignmentEntityToAssignmentMapper.map(assignmentEntity);

Optional<UserLocationEntity> optionalUserLocationEntity = userLocationRepository
.findByUserId(assignmentEntity.getUserId());

if (optionalUserLocationEntity.isPresent()) {

UserLocation userLocation = userLocationEntityToUserLocationMapper
.map(optionalUserLocationEntity.get());

assignment.getUser().setLocation(userLocation.getPoint());
}

return assignment;
}

}
19 changes: 17 additions & 2 deletions src/main/java/com/ays/user/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import com.ays.user.model.enums.UserRole;
import com.ays.user.model.enums.UserStatus;
import com.ays.user.model.enums.UserSupportStatus;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.locationtech.jts.geom.Point;

/**
* User Domain Model to perform data transfer from service layer to controller
Expand All @@ -27,7 +27,22 @@ public class User extends BaseDomainModel {
private UserStatus status;
private UserSupportStatus supportStatus;
private AysPhoneNumber phoneNumber;
private Location location;

private Institution institution;

@Getter
@Builder
public static class Location {
private Double longitude;
private Double latitude;
}

public void setLocation(Point point) {
this.location = Location.builder()
.longitude(point.getX())
.latitude(point.getY())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
import com.ays.assignment.util.exception.AysAssignmentNotExistByIdException;
import com.ays.auth.model.AysIdentity;
import com.ays.common.util.AysRandomUtil;
import com.ays.location.model.UserLocation;
import com.ays.location.model.entity.UserLocationEntity;
import com.ays.location.model.entity.UserLocationEntityBuilder;
import com.ays.location.model.mapper.UserLocationEntityToUserLocationMapper;
import com.ays.location.repository.UserLocationRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
Expand All @@ -26,11 +31,14 @@ class AssignmentServiceImplTest extends AbstractUnitTest {
@Mock
private AssignmentRepository assignmentRepository;

@Mock
private UserLocationRepository userLocationRepository;

@Mock
private AysIdentity identity;

private static final AssignmentEntityToAssignmentMapper ASSIGNMENT_ENTITY_TO_ASSIGNMENT_MAPPER = AssignmentEntityToAssignmentMapper.initialize();

private static final UserLocationEntityToUserLocationMapper USER_LOCATION_ENTITY_TO_USER_LOCATION_MAPPER = UserLocationEntityToUserLocationMapper.initialize();

@Test
void givenAssignmentId_whenGetAssignment_thenReturnAssignment() {
Expand All @@ -42,14 +50,18 @@ void givenAssignmentId_whenGetAssignment_thenReturnAssignment() {
.withValidFields()
.withInstitutionId(mockInstitutionId).build();
String mockAssignmentId = mockAssignmentEntity.getId();
UserLocationEntity mockUserLocationEntity = new UserLocationEntityBuilder().withValidFields().build();

UserLocation mockUserLocation = USER_LOCATION_ENTITY_TO_USER_LOCATION_MAPPER.map(mockUserLocationEntity);
Assignment mockAssignment = ASSIGNMENT_ENTITY_TO_ASSIGNMENT_MAPPER.map(mockAssignmentEntity);
mockAssignment.getUser().setLocation(mockUserLocation.getPoint());

// When
Mockito.when(identity.getInstitutionId())
.thenReturn(mockInstitutionId);
Mockito.when(assignmentRepository.findByIdAndInstitutionId(Mockito.anyString(), Mockito.anyString()))
.thenReturn(Optional.of(mockAssignmentEntity));
Mockito.when(userLocationRepository.findByUserId(Mockito.anyString())).thenReturn(Optional.of(mockUserLocationEntity));

// Then
Assignment assignment = assignmentService.getAssignmentById(mockAssignmentId);
Expand All @@ -61,6 +73,10 @@ void givenAssignmentId_whenGetAssignment_thenReturnAssignment() {
Assertions.assertEquals(mockAssignment.getPhoneNumber().getLineNumber(), assignment.getPhoneNumber().getLineNumber());
Assertions.assertEquals(mockAssignment.getStatus(), assignment.getStatus());
Assertions.assertEquals(mockAssignment.getPoint(), assignment.getPoint());
Assertions.assertEquals(mockAssignment.getUser().getLocation().getLongitude(),
assignment.getUser().getLocation().getLongitude());
Assertions.assertEquals(mockAssignment.getUser().getLocation().getLatitude(),
assignment.getUser().getLocation().getLatitude());

Mockito.verify(assignmentRepository, Mockito.times(1))
.findByIdAndInstitutionId(mockAssignmentId, mockInstitutionId);
Expand Down Expand Up @@ -93,4 +109,5 @@ void givenAssignmentId_whenAssignmentNotFound_thenThrowAysAssignmentNotExistById
Mockito.verify(identity, Mockito.times(1))
.getInstitutionId();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.ays.location.model.entity;

import com.ays.common.model.TestDataBuilder;
import com.ays.user.model.entity.UserEntity;
import com.ays.user.model.entity.UserEntityBuilder;

public class UserLocationEntityBuilder extends TestDataBuilder<UserLocationEntity> {

public UserLocationEntityBuilder() {
super(UserLocationEntity.class);
}

public UserLocationEntityBuilder withValidFields() {

UserEntity userEntity = new UserEntityBuilder()
.withValidFields()
.build();

return this
.withId(1L)
.withUserId(userEntity.getId())
.withUser(userEntity)
.withPoint(1.0, 2.0);
}

public UserLocationEntityBuilder withId(Long id) {
data.setId(id);
return this;
}

public UserLocationEntityBuilder withUserId(String userId) {
data.setUserId(userId);
return this;
}

public UserLocationEntityBuilder withUser(UserEntity userEntity) {
data.setUser(userEntity);
return this;
}

public UserLocationEntityBuilder withPoint(Double longitude, Double latitude) {
data.setPoint(longitude, latitude);
return this;
}

}

0 comments on commit af444a2

Please sign in to comment.