11import { TeamService } from "./team.service" ;
2+ import { TeamLogoDto } from "./teamLogo.dto" ;
23
34export class TeamController {
45 private teamService : TeamService ;
@@ -30,6 +31,26 @@ export class TeamController {
3031 }
3132 } ;
3233
34+ /**
35+ * Verify if the query param is either teamIds or userId.
36+ * If the query param is teamIds, it will call the getTeamsLogoByTeamsId function.
37+ * If the query param is userId, it will call the getTeamsLogoByUserId function.
38+ * If the query param is neither teamIds or userId, it will return a 400 error.
39+ * @param req
40+ * @param res
41+ */
42+ getTeamsLogo = async ( req : any , res : any ) => {
43+ const queryParams = Object . keys ( req . query ) [ 0 ] ;
44+
45+ if ( queryParams === "teamIds" ) {
46+ this . getTeamsLogoByTeamsId ( req , res ) ;
47+ } else if ( queryParams === "userId" ) {
48+ this . getTeamsLogoByUserId ( req , res ) ;
49+ } else {
50+ res . status ( 400 ) . json ( { message : "invalid query param" } ) ;
51+ }
52+ } ;
53+
3354 /**
3455 * Retrive a list of team logo given a list of team id.
3556 * If a team id does not have a logo, it will not be returned.
@@ -38,13 +59,12 @@ export class TeamController {
3859 * @param req
3960 * @param res
4061 */
41- getTeamImageByTeamsId = async ( req : any , res : any ) => {
62+ getTeamsLogoByTeamsId = async ( req : any , res : any ) => {
4263 const teamIds = req . query . teamIds ;
43-
4464 const idsArray = this . teamsIdsValidation ( teamIds ) ;
4565
4666 try {
47- const teamsLogo = await this . teamService . getTeamLogoByTeamsId ( idsArray ) ;
67+ const teamsLogo = await this . teamService . getTeamsLogoByTeamsId ( idsArray ) ;
4868
4969 if ( teamsLogo . length === 0 ) {
5070 console . log ( "404 no team logo was found" ) ;
@@ -82,4 +102,37 @@ export class TeamController {
82102
83103 return idsArray ;
84104 } ;
105+
106+ /**
107+ * Retrive a list of team logo given a user id, meaning its all teams the user is into.
108+ * If a team id does not have a logo, it will not be returned.
109+ * User query param, ex : ?userId=1
110+ * @param req
111+ * @param res
112+ */
113+ getTeamsLogoByUserId = async ( req : any , res : any ) => {
114+ const userId = Number ( req . query . userId ) ;
115+
116+ if ( isNaN ( userId ) ) {
117+ throw new Error ( "invalid user id provided" ) ;
118+ }
119+
120+ try {
121+ const teamsLogo = await this . teamService . getTeamsLogoByUserId ( userId ) ;
122+
123+ if ( teamsLogo . length === 0 ) {
124+ console . log ( "404 no team logo was found" ) ;
125+ res . status ( 404 ) . json ( { message : "No team logo was found" } ) ;
126+ } else {
127+ console . log (
128+ "200 team logo was found for the teams :" ,
129+ teamsLogo . map ( ( team : TeamLogoDto ) => team . teamLogoTeamId )
130+ ) ;
131+ res . status ( 200 ) . json ( teamsLogo ) ;
132+ }
133+ } catch ( error : any ) {
134+ // catch server errors
135+ res . status ( 500 ) . json ( { message : error . message } ) ;
136+ }
137+ } ;
85138}
0 commit comments