Skip to content

Commit

Permalink
[feat]: add MatchApiService
Browse files Browse the repository at this point in the history
  • Loading branch information
llucasspot committed Jan 21, 2024
1 parent 0417d7d commit c08b656
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 55 deletions.
24 changes: 24 additions & 0 deletions expo-boomboomapp/src/api/MatchApiService/MatchApiMockService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { singleton } from "tsyringe";
import { v4 as uuidv4 } from "uuid";

import { MatchApiServiceI } from "./MatchApiServiceI";
import { user_helena, user_isabella, user_jessica } from "../../mocks/mokes";
import { StackProfileI } from "../ProfileApiService/ProfileApiServiceI";

@singleton()
export class MatchApiMockService implements MatchApiServiceI {
async getProfiles(): Promise<StackProfileI[]> {
const stackProfiles = [user_isabella, user_helena, user_jessica];
return Promise.resolve(
stackProfiles.map(({ user, songs }) => {
return {
user: {
...user,
id: uuidv4(),
},
songs,
};
}),
);
}
}
30 changes: 30 additions & 0 deletions expo-boomboomapp/src/api/MatchApiService/MatchApiService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { inject, singleton } from "tsyringe";

import { MatchApiServiceI } from "./MatchApiServiceI";
import ConfigurationService from "../../services/ConfigurationService/ConfigurationService";
import ErrorService from "../../services/ErrorService/ErrorService";
import StorageService from "../../services/StorageService/StorageService";
import ServiceInterface from "../../tsyringe/ServiceInterface";
import { ApiService } from "../ApiService";
import { StackProfileI } from "../ProfileApiService/ProfileApiServiceI";

@singleton()
export class MatchApiService extends ApiService implements MatchApiServiceI {
constructor(
@inject(ServiceInterface.StorageServiceI)
protected storageService: StorageService,
@inject(ServiceInterface.ConfigurationService)
protected configurationService: ConfigurationService,
@inject(ServiceInterface.ErrorService)
protected errorService: ErrorService,
) {
super("matches", storageService, configurationService, errorService);
}

async getProfiles(): Promise<StackProfileI[]> {
const res = await this.apiRequester.get<StackProfileI[]>("/");
console.log(res.data[0].user);
console.log(res.data[0].songs);
return res.data;
}
}
5 changes: 5 additions & 0 deletions expo-boomboomapp/src/api/MatchApiService/MatchApiServiceI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { StackProfileI } from "../ProfileApiService/ProfileApiServiceI";

export abstract class MatchApiServiceI {
abstract getProfiles(): Promise<StackProfileI[]>;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { singleton } from "tsyringe";
import { v4 as uuidv4 } from "uuid";

import {
CreateProfileBody,
EditProfileBody,
ProfileApiServiceI,
StackProfileI,
} from "./ProfileApiServiceI";
import {
user_helena,
user_isabella,
user_jessica,
user_yohan,
} from "../../mocks/mokes";
import { user_yohan } from "../../mocks/mokes";

@singleton()
export class ProfileApiMockService implements ProfileApiServiceI {
Expand All @@ -24,20 +17,6 @@ export class ProfileApiMockService implements ProfileApiServiceI {
return Promise.resolve(user_yohan);
}

async getStackProfiles(): Promise<StackProfileI[]> {
const stackProfiles = [user_isabella, user_helena, user_jessica];
return Promise.resolve(
stackProfiles.map(({ user, songs }) => {
return {
user: {
...user,
id: uuidv4(),
},
songs,
};
}),
);
}
async editProfile(editedProfileBody: EditProfileBody) {
return Promise.resolve({ ...user_yohan, ...editedProfileBody });
}
Expand Down
11 changes: 2 additions & 9 deletions expo-boomboomapp/src/api/ProfileApiService/ProfileApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { inject, singleton } from "tsyringe";

import {
CreateProfileBody,
ProfileI,
ProfileApiServiceI,
StackProfileI,
EditProfileBody,
ProfileApiServiceI,
ProfileI,
} from "./ProfileApiServiceI";
import ConfigurationService from "../../services/ConfigurationService/ConfigurationService";
import ErrorService from "../../services/ErrorService/ErrorService";
Expand Down Expand Up @@ -40,12 +39,6 @@ export class ProfileApiService
return res.data;
}

async getStackProfiles() {
// TODO url
const res = await this.apiRequester.get<StackProfileI[]>("/TODO");
return res.data;
}

async editProfile(editedProfileBody: EditProfileBody) {
// TODO url
const res = await this.apiRequester.put<ProfileI>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ export abstract class ProfileApiServiceI {

abstract getProfile(): Promise<ProfileI>;

abstract getStackProfiles(): Promise<StackProfileI[]>;

abstract editProfile(editedProfileBody: EditProfileBody): Promise<ProfileI>;

abstract uploadAvatar(uri: string): Promise<void>;
Expand Down
4 changes: 2 additions & 2 deletions expo-boomboomapp/src/components/matching/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React, { useEffect, useRef } from "react";
import { Animated } from "react-native";

import { CardContent } from "./CardContent";
import { Profile } from "./beans/Profile";
import { Observer } from "./hooks/useObserver";
import { StackProfileI } from "../../api/ProfileApiService/ProfileApiServiceI";
import { useCoreStyles } from "../../services/StyleService/styles";

type CardProps = {
index: number;
profile: Profile;
profile: StackProfileI;
onNext: Observer;
setCurrentIdBackground: React.Dispatch<React.SetStateAction<string | null>>;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import {
import { SafeAreaView } from "react-native-safe-area-context";

import { IMAGES } from "../../../../assets/assets";
import {
ProfileApiServiceI,
StackProfileI,
} from "../../../api/ProfileApiService/ProfileApiServiceI";
import { MatchApiServiceI } from "../../../api/MatchApiService/MatchApiServiceI";
import { StackProfileI } from "../../../api/ProfileApiService/ProfileApiServiceI";
import { Track } from "../../../api/SpotifyApiService/SpotifyApiServiceI";
import { BlurredBackground } from "../../../components/matching/BlurredBackground";
import { Card } from "../../../components/matching/Card";
Expand All @@ -33,15 +31,15 @@ type HomeScreenProps = NativeStackScreenProps<
>;

export function HomeScreen({}: HomeScreenProps): JSX.Element {
const profileApiService = getGlobalInstance<ProfileApiServiceI>(
ServiceInterface.ProfileApiServiceI,
const matchApiService = getGlobalInstance<MatchApiServiceI>(
ServiceInterface.MatchApiServiceI,
);

const [stackProfiles, setStackProfiles] = useState<StackProfileI[]>([]);

useEffect(() => {
profileApiService
.getStackProfiles()
matchApiService
.getProfiles()
.then((stackProfiles) => {
setStackProfiles(stackProfiles);
})
Expand Down Expand Up @@ -100,8 +98,8 @@ export function HomeScreen({}: HomeScreenProps): JSX.Element {
// On next, push a new profile in the stack
useEffect(() => {
const cb = onNextSubscriber.current.subscribe(() => {
profileApiService
.getStackProfiles()
matchApiService
.getProfiles()
.then((newStackProfiles) => {
setStackProfiles((stackProfiles) => [
...stackProfiles,
Expand Down
7 changes: 5 additions & 2 deletions expo-boomboomapp/src/tsyringe/ServiceInterface.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
enum ServiceInterface {
// api
SpotifyApiServiceI = "SpotifyApiServiceI",
ProfileApiServiceI = "ProfileApiServiceI",
MatchApiServiceI = "MatchApiServiceI",
// others
LanguageServiceI = "LanguageServiceI",
StorageServiceI = "StorageServiceI",
StyleServiceI = "StyleServiceI",
UserService = "UserService",
AuthService = "AuthService",
ErrorService = "ErrorService",
ConfigurationService = "ConfigurationService",
SpotifyApiServiceI = "SpotifyApiServiceI",
ProfileApiServiceI = "ProfileApiServiceI",
AppService = "AppService",
}

Expand Down
26 changes: 18 additions & 8 deletions expo-boomboomapp/src/tsyringe/tsyringe.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { container } from "tsyringe";

import ServiceInterface from "./ServiceInterface";
import { configureGlobalInjector, injectSingleton } from "./diUtils";
import { MatchApiMockService } from "../api/MatchApiService/MatchApiMockService";
import { MatchApiService } from "../api/MatchApiService/MatchApiService";
import { MatchApiServiceI } from "../api/MatchApiService/MatchApiServiceI";
import { ProfileApiMockService } from "../api/ProfileApiService/ProfileApiMockService";
import { ProfileApiService } from "../api/ProfileApiService/ProfileApiService";
import { ProfileApiServiceI } from "../api/ProfileApiService/ProfileApiServiceI";
Expand All @@ -22,6 +25,21 @@ const IS_APP_IN_MOCK_MODE = ConfigurationService.isAppInMockMode();
const injector = container.createChildContainer();
configureGlobalInjector(injector);

// api
injectSingleton<SpotifyApiServiceI>(
ServiceInterface.SpotifyApiServiceI,
IS_APP_IN_MOCK_MODE ? SpotifyApiMockService : SpotifyApiService,
);
injectSingleton<ProfileApiServiceI>(
ServiceInterface.ProfileApiServiceI,
IS_APP_IN_MOCK_MODE ? ProfileApiMockService : ProfileApiService,
);
injectSingleton<MatchApiServiceI>(
ServiceInterface.MatchApiServiceI,
IS_APP_IN_MOCK_MODE ? MatchApiMockService : MatchApiService,
);

// others
injectSingleton<StyleService>(ServiceInterface.StyleServiceI, StyleService);
injectSingleton<LanguageService>(
ServiceInterface.LanguageServiceI,
Expand All @@ -39,12 +57,4 @@ injectSingleton<AuthService>(ServiceInterface.AuthService, AuthService);
injectSingleton<AuthService>(ServiceInterface.AuthService, AuthService);
injectSingleton<UserService>(ServiceInterface.UserService, UserService);
injectSingleton<ErrorService>(ServiceInterface.ErrorService, ErrorService);
injectSingleton<SpotifyApiServiceI>(
ServiceInterface.SpotifyApiServiceI,
IS_APP_IN_MOCK_MODE ? SpotifyApiMockService : SpotifyApiService,
);
injectSingleton<ProfileApiServiceI>(
ServiceInterface.ProfileApiServiceI,
IS_APP_IN_MOCK_MODE ? ProfileApiMockService : ProfileApiService,
);
injectSingleton<AppService>(ServiceInterface.AppService, AppService);

0 comments on commit c08b656

Please sign in to comment.