-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sma 21 add endpoint that returns all sports #14
Merged
MatejFrnka
merged 13 commits into
develop
from
SMA-21_Add_endpoint_that_returns_all_sports
Feb 3, 2024
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9bb358b
Test problem
kz44 d4c0d40
SMA-21: Simplified tests
MatejFrnka 19caf17
feat:
kz44 651f561
Merge branch 'develop' of https://github.com/MatejFrnka/sportsmatch i…
kz44 7e00947
Merge branch 'develop' of https://github.com/MatejFrnka/sportsmatch i…
kz44 3ddc201
Feat: conflicts solved
kz44 b3ee042
Merge branch 'develop' of https://github.com/MatejFrnka/sportsmatch i…
kz44 9ce8759
Feat: conflicts solved
kz44 c569fb3
Feat: conflicts solved
kz44 278e5c9
Feat: conflicts solved
kz44 e0ad4a4
Feat: conflicts solved
kz44 e49d34e
Feat: formatting
kz44 6067e32
Feat: modified SportMapper to static method
kz44 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
backend/sportsmatch/src/main/java/com/sportsmatch/controllers/SportController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.sportsmatch.controllers; | ||
|
||
import com.sportsmatch.dtos.SportDTO; | ||
import com.sportsmatch.service.SportService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/api/v1/sports") | ||
public class SportController { | ||
|
||
private final SportService sportService; | ||
|
||
/** | ||
* This endpoint returns paginated list of SportDto. | ||
* | ||
* @param pageable contains tha page and size value | ||
* @return paginated list of SportDTO | ||
*/ | ||
@GetMapping("/all") | ||
public List<SportDTO> getSports(final Pageable pageable) { | ||
return sportService.getAllSports(pageable); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/sportsmatch/src/main/java/com/sportsmatch/dtos/SportDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.sportsmatch.dtos; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class SportDTO { | ||
|
||
public SportDTO(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String name; | ||
} |
27 changes: 27 additions & 0 deletions
27
backend/sportsmatch/src/main/java/com/sportsmatch/mappers/SportMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.sportsmatch.mappers; | ||
|
||
import com.sportsmatch.dtos.SportDTO; | ||
import com.sportsmatch.models.Sport; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Getter | ||
@Setter | ||
@RequiredArgsConstructor | ||
@Component | ||
public class SportMapper { | ||
|
||
/** | ||
* Converts a Sport entity to a SportDTO. | ||
* | ||
* @param entity The Sport entity to be converted. | ||
* @return SportDTO containing information from the Sport entity. | ||
*/ | ||
public static SportDTO toDTO (Sport entity) { | ||
return SportDTO.builder() | ||
.name(entity.getName()) | ||
.build(); | ||
} | ||
} |
5 changes: 1 addition & 4 deletions
5
backend/sportsmatch/src/main/java/com/sportsmatch/models/Sport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ | |
|
||
@Repository | ||
public interface SportRepository extends JpaRepository<Sport, Long> { | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
backend/sportsmatch/src/main/java/com/sportsmatch/service/SportService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.sportsmatch.service; | ||
|
||
import com.sportsmatch.dtos.SportDTO; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.List; | ||
|
||
public interface SportService { | ||
List<SportDTO> getAllSports(final Pageable pageable); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
backend/sportsmatch/src/main/java/com/sportsmatch/service/SportServiceImp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.sportsmatch.service; | ||
|
||
import com.sportsmatch.dtos.SportDTO; | ||
import com.sportsmatch.mappers.SportMapper; | ||
import com.sportsmatch.repos.SportRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class SportServiceImp implements SportService { | ||
|
||
private final SportRepository sportRepository; | ||
private final SportMapper sportMapper; | ||
|
||
|
||
/** | ||
* This method returns a paginated list of SportsDTO. | ||
* | ||
* @param pageable contains the page and size values for pagination. | ||
* @return paginated list of SportDTO. | ||
*/ | ||
public List<SportDTO> getAllSports(final Pageable pageable) { | ||
return sportRepository.findAll(pageable).stream() | ||
.map(SportMapper::toDTO) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
backend/sportsmatch/src/test/java/com/sportsmatch/service/SportServiceImpTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.sportsmatch.service; | ||
|
||
import com.sportsmatch.dtos.SportDTO; | ||
import com.sportsmatch.mappers.SportMapper; | ||
import com.sportsmatch.models.Sport; | ||
import com.sportsmatch.repos.SportRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
|
||
@SpringBootTest | ||
class SportServiceImpTest { | ||
|
||
@Mock | ||
private SportRepository sportRepository; | ||
|
||
@InjectMocks | ||
private SportServiceImp sportService; | ||
|
||
@Test | ||
void getAllSportsShouldReturnAllSportsWhenRequired() { | ||
// Arrange | ||
Pageable pageable = Mockito.mock(Pageable.class); | ||
|
||
Sport sport1 = new Sport("Football"); | ||
Sport sport2 = new Sport("Basketball"); | ||
|
||
List<Sport> sports = Arrays.asList(sport1, sport2); | ||
Page<Sport> sportsPage = new PageImpl<>(sports, pageable, sports.size()); | ||
|
||
SportDTO sportDTO1 = new SportDTO("Football"); | ||
SportDTO sportDTO2 = new SportDTO("Basketball"); | ||
|
||
List<SportDTO> expectedSportDTOs = Arrays.asList(sportDTO1, sportDTO2); | ||
|
||
// Mocking repository | ||
Mockito.when(sportRepository.findAll(Mockito.any(Pageable.class))).thenReturn(sportsPage); | ||
|
||
// Act | ||
List<SportDTO> result = sportService.getAllSports(pageable); | ||
|
||
// Assert | ||
assertEquals(expectedSportDTOs, result); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realised that we might not need pagination for sports because there will probably only be at most 50 different sports. But lets keep it for now as an example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I miss understood it sorry. I thought later there will be someting filter for it too like search in it or something like that