Skip to content

Commit

Permalink
User's vue controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ro0t-set committed Sep 7, 2023
1 parent b899f65 commit 58da2a3
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 81 deletions.
13 changes: 8 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions services/frontend/src/services/users/auth/auth-controller-impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { AuthController } from './auth-controller'
import axios from 'axios'

export class AuthControllerImpl implements AuthController {
async register(
username: string,
email: string,
password: string,
description: string | null,
photo: string | null
): Promise<string> {
const data = {
username: username,
email: email,
password: password,
description: description,
photo: photo
}

const response = await axios.post('/auth/register', data)
if (response.status === 200) {
return response.data
} else if (response.status === 400) {
throw new Error('Bad requestd')
} else if (response.status === 409) {
throw new Error('UserAlreadyExists')
}

return Promise.resolve('register success')
}

async login(username: string, password: string): Promise<string> {
const data = {
username: username,
password: password
}

const response = await axios.post('/auth/login', data)
if (response.status === 200) {
return response.data
} else if (response.status === 400) {
throw new Error('Bad requestd')
} else if (response.status === 401) {
throw new Error('InvalidUsernameOrPassword')
}

return Promise.resolve('login success')
}

async logout(): Promise<void> {
const response = await axios.post('/auth/logout')
if (response.status === 200) {
console.log('logout success')
} else if (response.status === 401) {
throw new Error('Access token is missing or invalid')
}
return Promise.resolve()
}

async refreshToken(): Promise<string> {
const response = await axios.post('/auth/refresh')
if (response.status === 200) {
return response.data
} else if (response.status === 401) {
throw new Error('InvalidRefreshToken')
}

return Promise.resolve('refresh success')
}
}
41 changes: 41 additions & 0 deletions services/frontend/src/services/users/auth/auth-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export interface AuthController {
/**
* Register a new user.
* @param username The username of the user.
* @param email The email of the user.
* @param password The password of the user.
* @param description The description of the user.
* @param photo The profile picture of the user.
* @throws {UserAlreadyExists} If the user already exists.
*/
register(
username: string,
email: string,
password: string,
description: string | null,
photo: String | null
): Promise<string>
/**
* Login a user.
* @param username The username of the user.
* @param password The password of the user.
* @returns The access token.
* @throws {InvalidUsernameOrPassword} If the username or password is invalid.
*/
login(username: string, password: string): Promise<string>

/**
* Logout a user.
* @throws {UserNotFound} If the user is not found.
*/
logout(): Promise<void>

/**
* Refresh the access token of a user.
* @returns The new access token.
* @throws {UserNotFound} If the user is not found.
* @throws {RefreshTokenNotPresent} If the refresh token is not present.
* @throws {InvalidRefreshToken} If the refresh token is invalid.
*/
refreshToken(): Promise<string>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import type { FriendsController } from './friends-controller'
import axios from 'axios'

export class FriendsControllerImpl implements FriendsController {
async getFriends(): Promise<string[]> {
const response = await axios.get('/friends')
if (response.status === 200) {
return response.data
} else if (response.status === 401) {
throw new Error('Access token is missing or invalid')
}
return Promise.resolve([])
}

async getFriendsRequests(): Promise<string[]> {
const response = await axios.get('/friends/requests')
if (response.status === 200) {
return response.data
} else if (response.status === 401) {
throw new Error('Access token is missing or invalid')
}
return Promise.resolve([])
}

async sendFriendRequest(friendUsername: string): Promise<void> {
const data = {
friendUsername: friendUsername
}

const response = await axios.post('/friends/requests', data)
if (response.status === 200) {
return response.data
} else if (response.status === 400) {
throw new Error('Bad requestd')
} else if (response.status === 401) {
throw new Error('Access token is missing or invalid')
} else if (response.status === 404) {
throw new Error('UserNotFound')
}
return Promise.resolve()
}

async acceptFriendRequest(friendUsername: string): Promise<void> {
const data = {
friendUsername: friendUsername,
action: 'accept'
}

const response = await axios.post('/friends/requests', data)
if (response.status === 200) {
return response.data
} else if (response.status === 400) {
throw new Error('Bad requestd')
} else if (response.status === 401) {
throw new Error('Access token is missing or invalid')
} else if (response.status === 404) {
throw new Error('UserNotFound')
} else if (response.status === 409) {
throw new Error('FriendRequestNotPresent')
}
return Promise.resolve()
}

async denyFriendRequest(friendUsername: string): Promise<void> {
const data = {
friendUsername: friendUsername,
action: 'deny'
}

const response = await axios.post('/friends/requests', data)
if (response.status === 200) {
return response.data
} else if (response.status === 400) {
throw new Error('Bad requestd')
} else if (response.status === 401) {
throw new Error('Access token is missing or invalid')
} else if (response.status === 404) {
throw new Error('UserNotFound')
} else if (response.status === 409) {
throw new Error('FriendRequestNotPresent')
}
return Promise.resolve()
}
}
41 changes: 41 additions & 0 deletions services/frontend/src/services/users/friends/friends-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export interface FriendsController {
/**
* Get the friends of a user.
* @returns The friends of the user.
* @throws {UserNotFound} If the user is not found.
*/
getFriends(): Promise<string[]>

/**
* Get friend's requests
* @returns The friend's requests of the user.
* @throws {UserNotFound} If the user is not found.
*/
getFriendsRequests(): Promise<string[]>

/**
* Send a friend request to a user.
* @param friendUsername The username of the friend.
* @throws {UserNotFound} If the user or the friend is not found.
* @throws {FriendRequestAlreadySent} If the friend request is already sent.
*/
sendFriendRequest(friendUsername: string): Promise<void>

/**
* Accept a friend request from a user.
* @param friendUsername The username of the friend.
* @throws {UserNotFound} If the user is not found.
* @throws {UserNotFound} If the friend is not found.
* @throws {FriendRequestNotPresent} If the friend request is not present.
*/
acceptFriendRequest(friendUsername: string): Promise<void>

/**
* Deny a friend request from a user.
* @param friendUsername The username of the friend.
* @throws {UserNotFound} If the user is not found.
* @throws {UserNotFound} If the friend is not found.
* @throws {FriendRequestNotPresent} If the friend request is not present.
*/
denyFriendRequest(friendUsername: string): Promise<void>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { ProfileController } from './profile-controller'
import axios from 'axios'

export class ProfileControllerImpl implements ProfileController {
async updateUserPhoto(photo: string): Promise<void> {
const data = {
photo: photo
}

const response = await axios.put('/profile/photo', data)
if (response.status === 200) {
console.log('update photo success')
} else if (response.status === 400) {
throw new Error('Bad requestd')
} else if (response.status === 401) {
throw new Error('Access token is missing or invalid')
}
return Promise.resolve()
}
async updateUserDescription(description: string): Promise<void> {
const data = {
description: description
}

const response = await axios.put('/profile/description', data)
if (response.status === 200) {
console.log('update description success')
} else if (response.status === 400) {
throw new Error('Bad requestd')
} else if (response.status === 401) {
throw new Error('Access token is missing or invalid')
}
return Promise.resolve()
}
}
13 changes: 13 additions & 0 deletions services/frontend/src/services/users/profile/profile-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface ProfileController {
/**
* Set the photo of a user.
* @param photo The photo of the user.
*/
updateUserPhoto(photo: string): Promise<void>

/**
* Set the user's description.
* @param description The description of the user.
*/
updateUserDescription(description: string): Promise<void>
}
41 changes: 0 additions & 41 deletions services/frontend/src/services/users/user-requests-impl.ts

This file was deleted.

Loading

0 comments on commit 58da2a3

Please sign in to comment.