Skip to content
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
merged 13 commits into from
Feb 3, 2024
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) {
Copy link
Owner

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

Copy link
Collaborator Author

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

return sportService.getAllSports(pageable);
}
}
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;
}
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.sportsmatch.models;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import java.util.HashSet;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

@Repository
public interface SportRepository extends JpaRepository<Sport, Long> {

}
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);

}
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());
}
}
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);
}
}