Skip to content

Commit 2c2ca04

Browse files
committed
Added Swagger Documentation
1 parent 1a2720a commit 2c2ca04

File tree

9 files changed

+141
-25
lines changed

9 files changed

+141
-25
lines changed

iplserverapp/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<groupId>org.springframework.boot</groupId>
2626
<artifactId>spring-boot-starter-data-mongodb</artifactId>
2727
</dependency>
28-
28+
2929
<dependency>
3030
<groupId>org.springframework.boot</groupId>
3131
<artifactId>spring-boot-starter-actuator</artifactId>
@@ -45,6 +45,18 @@
4545
<artifactId>lombok</artifactId>
4646
<optional>true</optional>
4747
</dependency>
48+
49+
<dependency>
50+
<groupId>io.springfox</groupId>
51+
<artifactId>springfox-swagger2</artifactId>
52+
<version>2.9.2</version>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>io.springfox</groupId>
57+
<artifactId>springfox-swagger-ui</artifactId>
58+
<version>2.9.2</version>
59+
</dependency>
4860
<dependency>
4961
<groupId>org.springframework.boot</groupId>
5062
<artifactId>spring-boot-starter-test</artifactId>

iplserverapp/src/main/java/com/lwl/iplserverapp/IplserverappApplication.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.lwl.iplserverapp;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
35
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.boot.CommandLineRunner;
57
import org.springframework.boot.SpringApplication;
@@ -8,7 +10,8 @@
810

911
@SpringBootApplication
1012
public class IplserverappApplication implements CommandLineRunner {
11-
13+
14+
private Logger log = LoggerFactory.getLogger(IplserverappApplication.class);
1215
@Autowired
1316
private MongoOperations mongoOpe;
1417

@@ -18,6 +21,6 @@ public static void main(String[] args) {
1821

1922
@Override
2023
public void run(String... args) throws Exception {
21-
24+
2225
}
2326
}
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
11
package com.lwl.iplserverapp.config;
22

3-
import org.springframework.beans.factory.annotation.Value;
43
import org.springframework.context.annotation.Bean;
54
import org.springframework.context.annotation.Configuration;
6-
import org.springframework.data.mongodb.core.MongoOperations;
7-
import org.springframework.data.mongodb.core.MongoTemplate;
85

9-
import com.mongodb.client.MongoClient;
10-
import com.mongodb.client.MongoClients;
6+
import springfox.documentation.builders.ApiInfoBuilder;
7+
import springfox.documentation.builders.PathSelectors;
8+
import springfox.documentation.builders.RequestHandlerSelectors;
9+
import springfox.documentation.service.ApiInfo;
10+
import springfox.documentation.spi.DocumentationType;
11+
import springfox.documentation.spring.web.plugins.Docket;
12+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
1113

1214
@Configuration
15+
@EnableSwagger2
1316
public class AppConfig {
1417

18+
19+
@Bean
20+
public Docket api() {
21+
return new Docket(DocumentationType.SWAGGER_2)
22+
.select()
23+
.apis(RequestHandlerSelectors.basePackage("com.lwl.iplserverapp"))
24+
.paths(PathSelectors.any())
25+
.build().apiInfo(apiInfo());
26+
}
27+
28+
public ApiInfo apiInfo() {
29+
return new ApiInfoBuilder().title("IPL Stat Application").build();
30+
}
31+
1532
}

iplserverapp/src/main/java/com/lwl/iplserverapp/dao/TeamDAO.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
public interface TeamDAO {
1010

11-
public TeamLabelDTO getTeamLabels();
12-
public List<PlayerDTO> getPlayersByTeam(String label);
13-
public List<RoleCountDTO> getRoleCountByTeam(String label);
14-
public List<PlayerDTO> getPlayerByTeamAndRole(String label,String role);
11+
public TeamLabelDTO selectTeamLabels();
12+
public List<PlayerDTO> selectPlayersByTeam(String label);
13+
public List<RoleCountDTO> selectRoleCountByTeam(String label);
14+
public List<PlayerDTO> selectPlayerByTeamAndRole(String label,String role);
1515

1616
}

iplserverapp/src/main/java/com/lwl/iplserverapp/dao/TeamDAOImpl.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class TeamDAOImpl implements TeamDAO {
3131
private MongoOperations mongoOpe;
3232

3333
@Override
34-
public TeamLabelDTO getTeamLabels() {
34+
public TeamLabelDTO selectTeamLabels() {
3535
GroupOperation group = group().addToSet("_id").as("labels");
3636
ProjectionOperation project = project().and("labels").as("labels").andExclude("_id");
3737
Aggregation agg = Aggregation.newAggregation(group, project);
@@ -41,7 +41,7 @@ public TeamLabelDTO getTeamLabels() {
4141
}
4242

4343
@Override
44-
public List<PlayerDTO> getPlayersByTeam(String label) {
44+
public List<PlayerDTO> selectPlayersByTeam(String label) {
4545
MatchOperation match = match(Criteria.where("_id").is(label));
4646
UnwindOperation unwind = unwind("players");
4747
ProjectionOperation project = project().and("players.name").as("name").and("players.role").as("role")
@@ -50,20 +50,38 @@ public List<PlayerDTO> getPlayersByTeam(String label) {
5050
Aggregation aggQuery = Aggregation.newAggregation(match, unwind, project);
5151
AggregationResults<PlayerDTO> result = mongoOpe.aggregate(aggQuery, "team", PlayerDTO.class);
5252
List<PlayerDTO> players = result.getMappedResults();
53-
log.info("Total player found for team {} is {}",label,players.size());
53+
log.info("Total player found for team {} is {}", label, players.size());
5454
return players;
5555
}
5656

5757
@Override
58-
public List<RoleCountDTO> getRoleCountByTeam(String label) {
59-
// TODO Auto-generated method stub
60-
return null;
58+
public List<RoleCountDTO> selectRoleCountByTeam(String label) {
59+
MatchOperation match = match(Criteria.where("_id").is("CSK"));
60+
UnwindOperation unwind = unwind("players");
61+
GroupOperation group = group("players.role").count().as("count");
62+
ProjectionOperation project = project().and("_id").as("roleName").and("count").as("count").andExclude("_id");
63+
Aggregation agg = Aggregation.newAggregation(match, unwind, group, project);
64+
AggregationResults<RoleCountDTO> res = mongoOpe.aggregate(agg, "team", RoleCountDTO.class);
65+
List<RoleCountDTO> list = res.getMappedResults();
66+
log.info("Total roles found in DB:{}" , list.size());
67+
return list;
6168
}
6269

6370
@Override
64-
public List<PlayerDTO> getPlayerByTeamAndRole(String label, String role){
65-
// TODO Auto-generated method stub
66-
return null;
71+
public List<PlayerDTO> selectPlayerByTeamAndRole(String label, String role) {
72+
73+
MatchOperation match1 = match(Criteria.where("_id").is(label));
74+
UnwindOperation unwind = unwind("players");
75+
MatchOperation match2 = match(Criteria.where("players.role").is(role));
76+
ProjectionOperation project = project().and("players.name").as("name").and("players.role").as("role")
77+
.and("players.price").as("price").and("_id").as("label").andExclude("_id");
78+
79+
Aggregation aggQuery = Aggregation.newAggregation(match1, unwind,match2, project);
80+
AggregationResults<PlayerDTO> result = mongoOpe.aggregate(aggQuery, "team", PlayerDTO.class);
81+
List<PlayerDTO> players = result.getMappedResults();
82+
log.info("Total player found for team {} and for role: {} is {}", label, role, players.size());
83+
return players;
84+
6785
}
6886

6987
}

iplserverapp/src/main/java/com/lwl/iplserverapp/service/TeamService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.util.List;
44

55
import com.lwl.iplserverapp.domain.Team;
6+
import com.lwl.iplserverapp.dto.PlayerDTO;
7+
import com.lwl.iplserverapp.dto.RoleCountDTO;
8+
import com.lwl.iplserverapp.dto.TeamLabelDTO;
69

710
public interface TeamService {
811

@@ -12,4 +15,9 @@ public interface TeamService {
1215
public boolean removeTeam(String label);
1316
public Team updateTeam(Team team);
1417
public Team getTeam(String label);
18+
19+
public TeamLabelDTO getTeamLabels();
20+
public List<PlayerDTO> getPlayersByTeam(String label);
21+
public List<RoleCountDTO> getRoleCountByTeam(String label);
22+
public List<PlayerDTO> getPlayerByTeamAndRole(String label,String role);
1523
}

iplserverapp/src/main/java/com/lwl/iplserverapp/service/TeamServiceImpl.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import org.springframework.stereotype.Service;
1010
import org.springframework.util.Assert;
1111

12+
import com.lwl.iplserverapp.dao.TeamDAO;
1213
import com.lwl.iplserverapp.domain.Team;
14+
import com.lwl.iplserverapp.dto.PlayerDTO;
15+
import com.lwl.iplserverapp.dto.RoleCountDTO;
16+
import com.lwl.iplserverapp.dto.TeamLabelDTO;
1317
import com.lwl.iplserverapp.repo.TeamRepo;
1418
import com.lwl.iplserverapp.service.exception.TeamNotFoundException;
1519

@@ -23,6 +27,9 @@ public class TeamServiceImpl implements TeamService {
2327
@Autowired
2428
private TeamRepo teamRepo;
2529

30+
@Autowired
31+
private TeamDAO teamDao;
32+
2633
@Override
2734
public Team addTeam(Team team) {
2835
Assert.notNull(team, "Team can't be null");
@@ -59,7 +66,6 @@ public boolean removeTeam(String label) {
5966
throw new TeamNotFoundException(String.format(labelNotFoundMessage, label));
6067
}
6168
}
62-
6369

6470
@Override
6571
public Team updateTeam(Team team) {
@@ -82,4 +88,28 @@ public Team getTeam(String label) {
8288
return optTeam.orElseThrow(() -> new TeamNotFoundException(String.format(labelNotFoundMessage, label)));
8389
}
8490

91+
@Override
92+
public TeamLabelDTO getTeamLabels() {
93+
return teamDao.selectTeamLabels();
94+
}
95+
96+
@Override
97+
public List<PlayerDTO> getPlayersByTeam(String label) {
98+
Assert.notNull(label, "Team label can't be null or empty");
99+
return teamDao.selectPlayersByTeam(label);
100+
}
101+
102+
@Override
103+
public List<RoleCountDTO> getRoleCountByTeam(String label) {
104+
Assert.notNull(label, "Team label can't be null or empty");
105+
return teamDao.selectRoleCountByTeam(label);
106+
}
107+
108+
@Override
109+
public List<PlayerDTO> getPlayerByTeamAndRole(String label, String role) {
110+
Assert.notNull(label, "Team label can't be null or empty");
111+
Assert.notNull(label, "Team label can't be null or empty");
112+
return teamDao.selectPlayerByTeamAndRole(label, role);
113+
}
114+
85115
}

iplserverapp/src/main/java/com/lwl/iplserverapp/web/TeamController.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.lwl.iplserverapp.web;
22

33
import java.util.List;
4+
5+
import javax.websocket.server.PathParam;
6+
47
import org.springframework.beans.factory.annotation.Autowired;
58
import org.springframework.web.bind.annotation.DeleteMapping;
69
import org.springframework.web.bind.annotation.GetMapping;
@@ -11,7 +14,11 @@
1114
import org.springframework.web.bind.annotation.RequestMapping;
1215
import org.springframework.web.bind.annotation.RequestParam;
1316
import org.springframework.web.bind.annotation.RestController;
17+
1418
import com.lwl.iplserverapp.domain.Team;
19+
import com.lwl.iplserverapp.dto.PlayerDTO;
20+
import com.lwl.iplserverapp.dto.RoleCountDTO;
21+
import com.lwl.iplserverapp.dto.TeamLabelDTO;
1522
import com.lwl.iplserverapp.service.TeamService;
1623

1724
@RestController
@@ -50,8 +57,26 @@ public boolean removeTeam(@PathVariable("label") String label) {
5057
public Team fetchTeam(@PathVariable("label") String label) {
5158
return teamService.getTeam(label);
5259
}
53-
54-
55-
60+
61+
@GetMapping("/team/labels")
62+
public TeamLabelDTO fetchTeamLabels() {
63+
return teamService.getTeamLabels();
64+
}
65+
66+
@GetMapping("/team/players/{label}")
67+
public List<PlayerDTO> fetchPlayerByTeam(@PathVariable("label") String label) {
68+
return teamService.getPlayersByTeam(label);
69+
}
70+
71+
@GetMapping("/team/players/{label}/{role}")
72+
public List<PlayerDTO> fetchPlayerByTeamAndRole(@PathVariable("label") String label,
73+
@PathVariable("role") String role) {
74+
return teamService.getPlayerByTeamAndRole(label, role);
75+
}
76+
77+
@GetMapping("/team/rolecount/{label}")
78+
public List<RoleCountDTO> fetchRoleCountByTeam(@PathVariable("label") String teamLabel) {
79+
return teamService.getRoleCountByTeam(teamLabel);
80+
}
5681

5782
}

iplserverapp/src/main/resources/iplqueries

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ db.getCollection('team').aggregate([
6767
_id: 0
6868
}
6969
}]
70+
71+
72+

0 commit comments

Comments
 (0)