Skip to content

Commit 1a2720a

Browse files
committed
Added DAO classes
1 parent 2ee99a5 commit 1a2720a

File tree

10 files changed

+215
-56
lines changed

10 files changed

+215
-56
lines changed
Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,23 @@
11
package com.lwl.iplserverapp;
22

3-
import java.io.IOException;
4-
import java.util.List;
5-
63
import org.springframework.beans.factory.annotation.Autowired;
74
import org.springframework.boot.CommandLineRunner;
85
import org.springframework.boot.SpringApplication;
96
import org.springframework.boot.autoconfigure.SpringBootApplication;
10-
import org.springframework.data.annotation.Id;
117
import org.springframework.data.mongodb.core.MongoOperations;
12-
import org.springframework.data.mongodb.core.MongoTemplate;
13-
import org.springframework.data.mongodb.core.query.Criteria;
14-
import org.springframework.data.mongodb.core.query.Query;
15-
import static org.springframework.data.mongodb.core.query.Criteria.where;
16-
import com.fasterxml.jackson.core.type.TypeReference;
17-
import com.fasterxml.jackson.databind.ObjectMapper;
18-
import com.lwl.iplserverapp.domain.Team;
19-
import com.lwl.iplserverapp.repo.TeamRepo;
20-
import com.mongodb.client.MongoClient;
21-
22-
import lombok.AllArgsConstructor;
23-
import lombok.Builder;
24-
import lombok.Getter;
25-
import lombok.NoArgsConstructor;
26-
import lombok.Setter;
27-
import lombok.ToString;
28-
29-
@Getter
30-
@Setter
31-
@AllArgsConstructor
32-
@NoArgsConstructor
33-
@Builder
34-
@ToString
35-
class User{
36-
@Id
37-
private String id;
38-
private String username;
39-
private String password;
40-
}
418

429
@SpringBootApplication
4310
public class IplserverappApplication implements CommandLineRunner {
4411

4512
@Autowired
46-
private TeamRepo teamRepo;
47-
13+
private MongoOperations mongoOpe;
14+
4815
public static void main(String[] args) {
4916
SpringApplication.run(IplserverappApplication.class, args);
5017
}
5118

5219
@Override
5320
public void run(String... args) throws Exception {
54-
List<Team> teams = teamRepo.findAll();
55-
teams.forEach(team->{
56-
System.out.println(team.getName());
57-
});
21+
5822
}
5923
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.lwl.iplserverapp.dao;
2+
3+
import java.util.List;
4+
5+
import com.lwl.iplserverapp.dto.PlayerDTO;
6+
import com.lwl.iplserverapp.dto.RoleCountDTO;
7+
import com.lwl.iplserverapp.dto.TeamLabelDTO;
8+
9+
public interface TeamDAO {
10+
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);
15+
16+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.lwl.iplserverapp.dao;
2+
3+
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
4+
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
5+
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
6+
import static org.springframework.data.mongodb.core.aggregation.Aggregation.unwind;
7+
8+
import java.util.List;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.data.mongodb.core.MongoOperations;
14+
import org.springframework.data.mongodb.core.aggregation.Aggregation;
15+
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
16+
import org.springframework.data.mongodb.core.aggregation.GroupOperation;
17+
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
18+
import org.springframework.data.mongodb.core.aggregation.ProjectionOperation;
19+
import org.springframework.data.mongodb.core.aggregation.UnwindOperation;
20+
import org.springframework.data.mongodb.core.query.Criteria;
21+
import org.springframework.stereotype.Repository;
22+
23+
import com.lwl.iplserverapp.dto.PlayerDTO;
24+
import com.lwl.iplserverapp.dto.RoleCountDTO;
25+
import com.lwl.iplserverapp.dto.TeamLabelDTO;
26+
27+
@Repository
28+
public class TeamDAOImpl implements TeamDAO {
29+
private Logger log = LoggerFactory.getLogger(TeamDAOImpl.class);
30+
@Autowired
31+
private MongoOperations mongoOpe;
32+
33+
@Override
34+
public TeamLabelDTO getTeamLabels() {
35+
GroupOperation group = group().addToSet("_id").as("labels");
36+
ProjectionOperation project = project().and("labels").as("labels").andExclude("_id");
37+
Aggregation agg = Aggregation.newAggregation(group, project);
38+
AggregationResults<TeamLabelDTO> res = mongoOpe.aggregate(agg, "team", TeamLabelDTO.class);
39+
TeamLabelDTO obj = res.getUniqueMappedResult();
40+
return obj;
41+
}
42+
43+
@Override
44+
public List<PlayerDTO> getPlayersByTeam(String label) {
45+
MatchOperation match = match(Criteria.where("_id").is(label));
46+
UnwindOperation unwind = unwind("players");
47+
ProjectionOperation project = project().and("players.name").as("name").and("players.role").as("role")
48+
.and("players.price").as("price").and("_id").as("label").andExclude("_id");
49+
50+
Aggregation aggQuery = Aggregation.newAggregation(match, unwind, project);
51+
AggregationResults<PlayerDTO> result = mongoOpe.aggregate(aggQuery, "team", PlayerDTO.class);
52+
List<PlayerDTO> players = result.getMappedResults();
53+
log.info("Total player found for team {} is {}",label,players.size());
54+
return players;
55+
}
56+
57+
@Override
58+
public List<RoleCountDTO> getRoleCountByTeam(String label) {
59+
// TODO Auto-generated method stub
60+
return null;
61+
}
62+
63+
@Override
64+
public List<PlayerDTO> getPlayerByTeamAndRole(String label, String role){
65+
// TODO Auto-generated method stub
66+
return null;
67+
}
68+
69+
}

iplserverapp/src/main/java/com/lwl/iplserverapp/domain/Player.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.lwl.iplserverapp.domain;
22

3+
import lombok.AllArgsConstructor;
34
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
46
import lombok.Setter;
57
import lombok.ToString;
68

79
@Getter
810
@Setter
911
@ToString
12+
@AllArgsConstructor
13+
@NoArgsConstructor
1014
public class Player {
1115

1216
private String name;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.lwl.iplserverapp.dto;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@Getter
8+
@Setter
9+
@ToString
10+
public class PlayerDTO {
11+
12+
private String name;
13+
private String role;
14+
private String label;
15+
private double price;
16+
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.lwl.iplserverapp.dto;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@Getter
8+
@Setter
9+
@ToString
10+
public class RoleCountDTO {
11+
12+
private String roleName;
13+
private int count;
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.lwl.iplserverapp.dto;
2+
3+
import java.util.List;
4+
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
import lombok.ToString;
8+
9+
@Getter
10+
@Setter
11+
@ToString
12+
public class TeamLabelDTO {
13+
14+
private List<String> labels;
15+
}

iplserverapp/src/main/java/com/lwl/iplserverapp/repo/TeamRepo.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@
77
import com.lwl.iplserverapp.domain.Team;
88

99
public interface TeamRepo extends MongoRepository<Team,String> {
10-
11-
1210
List<Team> findByNameLike(String str);
13-
1411
}
Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.lwl.iplserverapp.web;
22

33
import java.util.List;
4-
54
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.data.mongodb.repository.Query;
7-
import org.springframework.data.repository.query.Param;
85
import org.springframework.web.bind.annotation.DeleteMapping;
96
import org.springframework.web.bind.annotation.GetMapping;
107
import org.springframework.web.bind.annotation.PathVariable;
@@ -14,19 +11,16 @@
1411
import org.springframework.web.bind.annotation.RequestMapping;
1512
import org.springframework.web.bind.annotation.RequestParam;
1613
import org.springframework.web.bind.annotation.RestController;
17-
1814
import com.lwl.iplserverapp.domain.Team;
1915
import com.lwl.iplserverapp.service.TeamService;
2016

2117
@RestController
2218
@RequestMapping("/api/v1/ipl")
2319
public class TeamController {
24-
20+
2521
@Autowired
2622
private TeamService teamService;
2723

28-
29-
3024
@PostMapping("/addteam")
3125
public Team addTeam(@RequestBody Team team) {
3226
return teamService.addTeam(team);
@@ -36,28 +30,28 @@ public Team addTeam(@RequestBody Team team) {
3630
public List<Team> allTeams() {
3731
return teamService.getTeams();
3832
}
39-
40-
@GetMapping("/search/{str}")
41-
public List<Team> search(@PathVariable("str") String str){
33+
34+
@GetMapping("/search")
35+
public List<Team> search(@RequestParam String str) {
4236
return teamService.search(str);
4337
}
38+
4439
@PutMapping("/modify")
4540
public Team updateTeam(@RequestBody Team team) {
4641
return teamService.updateTeam(team);
4742
}
48-
43+
4944
@DeleteMapping("/remove/{label}")
5045
public boolean removeTeam(@PathVariable("label") String label) {
5146
return teamService.removeTeam(label);
5247
}
48+
5349
@GetMapping("/team/{label}")
5450
public Team fetchTeam(@PathVariable("label") String label) {
5551
return teamService.getTeam(label);
5652
}
5753

5854

5955

60-
61-
62-
56+
6357
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#1
2+
3+
[{
4+
$group: {
5+
_id: null,
6+
labels: {
7+
"$addToSet": "$_id"
8+
}
9+
}
10+
}]
11+
12+
#2
13+
14+
db.getCollection('team').aggregate([
15+
16+
{"$match":{_id:"MI"}},
17+
{
18+
"$unwind":"$players"
19+
},
20+
{
21+
"$project":{name:"$players.name",role:"$players.role",lable:"$_id",price:"$players.price",_id:0}
22+
}
23+
])
24+
25+
#3
26+
27+
[{
28+
$match: {
29+
"_id": "CSK"
30+
}
31+
}, {
32+
$unwind: "$players"
33+
}, {
34+
$group: {
35+
_id: "$players.role",
36+
count: {
37+
"$sum": 1
38+
}
39+
}
40+
}, {
41+
$project: {
42+
"role": "$_id",
43+
count: 1,
44+
_id: 0
45+
}
46+
}]
47+
48+
49+
#4
50+
51+
[{
52+
$match: {
53+
"_id": "CSK"
54+
}
55+
}, {
56+
$unwind: "$players"
57+
}, {
58+
$match: {
59+
"players.role": "Batsman"
60+
}
61+
}, {
62+
$project: {
63+
name: "$players.name",
64+
role: "$players.role",
65+
label: "$_id",
66+
price: "$players.price",
67+
_id: 0
68+
}
69+
}]

0 commit comments

Comments
 (0)