Merge branch 'julius-dev' of Software_Engineering_I/greenvironment-frontend into master
commit
8a3494f4a6
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,12 @@
|
||||
import {IUser} from './IUser';
|
||||
|
||||
export interface IChat {
|
||||
|
||||
id: number;
|
||||
|
||||
namespace: string;
|
||||
|
||||
members: IUser[];
|
||||
|
||||
messages: any[];
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
import {IGraphqlError} from './IGraphqlError';
|
||||
|
||||
export interface IErrorResponse {
|
||||
error: {
|
||||
errors: IGraphqlError[];
|
||||
data: any;
|
||||
};
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
export interface IGraphqlError {
|
||||
|
||||
message: string;
|
||||
|
||||
path: string[];
|
||||
|
||||
location: {line: number; column: number}[];
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
import {IUser} from './IUser';
|
||||
|
||||
export interface IGroup {
|
||||
|
||||
id: number;
|
||||
|
||||
name: string;
|
||||
|
||||
creator: IUser;
|
||||
|
||||
admins: IUser[];
|
||||
|
||||
members: IUser[];
|
||||
|
||||
chat: any;
|
||||
|
||||
events: any;
|
||||
|
||||
joined: boolean;
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
import {IUser} from './IUser';
|
||||
|
||||
export enum RequestType {
|
||||
FRIENDREQUEST = 'FRIENDREQUEST',
|
||||
GROUPINVITE = 'GROUPINVITE',
|
||||
EVENTINVITE = 'EVENTINVITE',
|
||||
}
|
||||
|
||||
export interface IRequest {
|
||||
|
||||
id: number;
|
||||
|
||||
sender: IUser;
|
||||
|
||||
receiver: IUser;
|
||||
|
||||
type: RequestType;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
import {IUser} from './IUser';
|
||||
import {IGroup} from './IGroup';
|
||||
|
||||
export interface ISearchResult {
|
||||
users: IUser[];
|
||||
|
||||
groups: IGroup[];
|
||||
|
||||
posts: any[];
|
||||
|
||||
events: any[];
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
export interface ISettings {
|
||||
darkmode?: boolean;
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
import {IGroup} from './IGroup';
|
||||
import {ISettings} from './ISettings';
|
||||
import {IChat} from './IChat';
|
||||
import {IRequest} from './IRequest';
|
||||
|
||||
export interface IUser {
|
||||
|
||||
id: number;
|
||||
|
||||
name: string;
|
||||
|
||||
handle: string;
|
||||
|
||||
email?: string;
|
||||
|
||||
profilePicture?: string;
|
||||
|
||||
settings?: string;
|
||||
|
||||
level: number;
|
||||
|
||||
points: number;
|
||||
|
||||
numberOfPosts: number;
|
||||
|
||||
postCount: number;
|
||||
|
||||
posts: any[];
|
||||
|
||||
chats: IChat[];
|
||||
|
||||
receivedRequests: IRequest[];
|
||||
|
||||
sentRequests: IRequest[];
|
||||
|
||||
joinedAt: string;
|
||||
|
||||
friendCount: number;
|
||||
|
||||
friends: IUser[];
|
||||
|
||||
groupCount: number;
|
||||
|
||||
groups: IGroup[];
|
||||
|
||||
eventCount: number;
|
||||
|
||||
events: any[];
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpHeaders} from '@angular/common/http';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export abstract class BaseService {
|
||||
protected headers: HttpHeaders;
|
||||
|
||||
protected constructor() {
|
||||
this.headers = new HttpHeaders();
|
||||
this.headers.set('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
@ -1,105 +1,79 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Headers, Http, Request} from '@angular/http';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Login} from '../../models/login';
|
||||
import {User} from 'src/app/models/user';
|
||||
import {DatasharingService} from '../datasharing.service';
|
||||
import {Router} from '@angular/router';
|
||||
import {environment} from 'src/environments/environment';
|
||||
import { FriendRequest } from 'src/app/models/friendRequest';
|
||||
import { FriendInfo } from 'src/app/models/friendinfo';
|
||||
import { GroupInfo } from 'src/app/models/groupinfo';
|
||||
import {IUser} from '../../models/interfaces/IUser';
|
||||
import {BaseService} from '../base.service';
|
||||
import {tap} from 'rxjs/operators';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class LoginService {
|
||||
|
||||
constructor(private http: Http, private data: DatasharingService, private router: Router) {
|
||||
}
|
||||
|
||||
public login(login: Login, errorCb: any) {
|
||||
|
||||
const headers = new Headers();
|
||||
headers.set('Content-Type', 'application/json');
|
||||
interface ILoginRequestResult {
|
||||
data: {
|
||||
login: IUser;
|
||||
};
|
||||
}
|
||||
|
||||
return this.http.post(environment.graphQLUrl, this.buildJson(login))
|
||||
.subscribe(response => {
|
||||
this.loginSuccess();
|
||||
this.updateUserInfo(response.json());
|
||||
}, errorCb
|
||||
);
|
||||
const graphqlQuery = `mutation($email: String!, $pwHash: String!) {
|
||||
login(email: $email, passwordHash: $pwHash) {
|
||||
id,
|
||||
name,
|
||||
email,
|
||||
handle,
|
||||
points,
|
||||
level,
|
||||
receivedRequests{id, sender{name, handle, id}},
|
||||
sentRequests{receiver{id}},
|
||||
friends {
|
||||
id,
|
||||
name,
|
||||
level
|
||||
},
|
||||
groups {
|
||||
id,
|
||||
name
|
||||
},
|
||||
chats{
|
||||
id
|
||||
},
|
||||
settings
|
||||
}
|
||||
}`;
|
||||
|
||||
public loginSuccess() {
|
||||
this.router.navigateByUrl('');
|
||||
}
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class LoginService extends BaseService {
|
||||
|
||||
public updateUserInfo(response: any) {
|
||||
const user: User = new User();
|
||||
let friendRequest: FriendRequest = new FriendRequest();
|
||||
user.loggedIn = true;
|
||||
user.userID = response.data.login.id;
|
||||
user.username = response.data.login.name;
|
||||
user.handle = response.data.login.handle;
|
||||
user.email = response.data.login.email;
|
||||
user.points = response.data.login.points;
|
||||
user.level = response.data.login.level;
|
||||
for (const friend of response.data.login.friends) {
|
||||
user.friends.push(new FriendInfo(friend.id, friend.name, friend.level));
|
||||
}
|
||||
for (const group of response.data.login.groups) {
|
||||
user.groups.push(new GroupInfo(group.id, group.name));
|
||||
}
|
||||
user.chatIDs = response.data.login.chats;
|
||||
for (const request of response.data.login.sentRequests) {
|
||||
user.sentRequestUserIDs.push(request.receiver.id);
|
||||
}
|
||||
for (const request of response.data.login.receivedRequests) {
|
||||
friendRequest = new FriendRequest();
|
||||
friendRequest.id = request.id;
|
||||
friendRequest.senderUserID = request.sender.id;
|
||||
friendRequest.senderUsername = request.sender.name;
|
||||
friendRequest.senderHandle = request.sender.handle;
|
||||
user.receivedRequests.push(friendRequest);
|
||||
}
|
||||
if (JSON.parse(response.data.login.settings).darkmode === 'true') {
|
||||
user.darkmode = true;
|
||||
}
|
||||
this.data.changeUserInfo(user);
|
||||
constructor(private http: HttpClient, private datasharingService: DatasharingService) {
|
||||
super();
|
||||
}
|
||||
|
||||
public buildJson(login: Login): any {
|
||||
const body = {
|
||||
query: `mutation($email: String!, $pwHash: String!) {
|
||||
login(email: $email, passwordHash: $pwHash) {
|
||||
id,
|
||||
name,
|
||||
email,
|
||||
handle,
|
||||
points,
|
||||
level,
|
||||
receivedRequests{id, sender{name, handle, id}},
|
||||
sentRequests{receiver{id}},
|
||||
friends {
|
||||
id,
|
||||
name,
|
||||
level
|
||||
},
|
||||
groups {
|
||||
id,
|
||||
name
|
||||
},
|
||||
chats{
|
||||
id
|
||||
},
|
||||
settings
|
||||
}
|
||||
}`
|
||||
, variables: {
|
||||
/**
|
||||
* Builds the body for the login request
|
||||
* @param login
|
||||
*/
|
||||
private static buildRequestBody(login: Login): any {
|
||||
return {
|
||||
query: graphqlQuery,
|
||||
variables: {
|
||||
email: login.email,
|
||||
pwHash: login.passwordHash,
|
||||
}
|
||||
};
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a login request and returns the data of the logged in user.
|
||||
* @param login
|
||||
*/
|
||||
public login(login: Login) {
|
||||
const body = LoginService.buildRequestBody(login);
|
||||
return this.http.post<ILoginRequestResult>(environment.graphQLUrl, body)
|
||||
.pipe(tap(response => {
|
||||
const user = new User();
|
||||
user.assignFromResponse(response.data.login);
|
||||
this.datasharingService.changeUserInfo(user);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
@ -1,71 +1,109 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Headers, Http} from '@angular/http';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {DatasharingService} from '../datasharing.service';
|
||||
import {Router} from '@angular/router';
|
||||
import {environment} from 'src/environments/environment';
|
||||
import { User } from 'src/app/models/user';
|
||||
import { GroupInfo } from 'src/app/models/groupinfo';
|
||||
import {User} from 'src/app/models/user';
|
||||
import {GroupInfo} from 'src/app/models/groupinfo';
|
||||
import {Observable} from 'rxjs';
|
||||
import {ISearchResult} from '../../models/interfaces/ISearchResult';
|
||||
import {environment} from '../../../environments/environment';
|
||||
import {BaseService} from '../base.service';
|
||||
import {tap} from 'rxjs/operators';
|
||||
|
||||
interface ISearchRequestResult {
|
||||
data: {
|
||||
search: ISearchResult;
|
||||
};
|
||||
}
|
||||
|
||||
const graphqlQuery = `query($query: String!, $first: Int, $offset: Int) {
|
||||
search(query:$query, first: $first, offset: $offset) {
|
||||
users{
|
||||
profilePicture,
|
||||
name,
|
||||
id,
|
||||
handle,
|
||||
points,
|
||||
level,
|
||||
friends {
|
||||
id
|
||||
}
|
||||
}
|
||||
groups{
|
||||
id
|
||||
name
|
||||
creator{id name handle}
|
||||
members{id name handle}
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class SearchService {
|
||||
export class SearchService extends BaseService {
|
||||
constructor(private http: HttpClient, private data: DatasharingService, private router: Router) {
|
||||
super();
|
||||
}
|
||||
|
||||
users: Array<User>;
|
||||
constructor(private http: Http, private data: DatasharingService, private router: Router) {
|
||||
users: User[];
|
||||
|
||||
/**
|
||||
* Builds the body for the request
|
||||
* @param query - the search query
|
||||
* @param first - the limit of elements to fetch
|
||||
* @param offset - offset
|
||||
*/
|
||||
private static buildRequestBody(query: String, first: number = 20, offset: number = 0): any {
|
||||
return {
|
||||
query: graphqlQuery,
|
||||
variables: {
|
||||
query,
|
||||
first,
|
||||
offset
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public renderUsers(pResponse: any): Array<User> {
|
||||
/**
|
||||
* Maps the users in the response to the user class
|
||||
* @param response
|
||||
*/
|
||||
public getUsersForResponse(response: ISearchRequestResult): User[] {
|
||||
const users = new Array<User>();
|
||||
for (const user of pResponse.data.search.users) {
|
||||
const pUser = new User();
|
||||
pUser.profilePicture = user.profilePicture;
|
||||
pUser.username = user.name;
|
||||
pUser.userID = user.id;
|
||||
pUser.handle = user.handle;
|
||||
pUser.points = user.points;
|
||||
pUser.level = user.level;
|
||||
pUser.friends = user.friends;
|
||||
users.push(pUser);
|
||||
for (const foundUser of response.data.search.users) {
|
||||
const user = new User();
|
||||
user.profilePicture = foundUser.profilePicture;
|
||||
user.username = foundUser.name;
|
||||
user.userID = foundUser.id;
|
||||
user.handle = foundUser.handle;
|
||||
user.points = foundUser.points;
|
||||
user.level = foundUser.level;
|
||||
// @ts-ignore
|
||||
user.friends = foundUser.friends;
|
||||
users.push(user);
|
||||
}
|
||||
return users;
|
||||
}
|
||||
|
||||
public renderGroups(pResponse: any): Array<GroupInfo> {
|
||||
/**
|
||||
* Maps the groups in the response to the group class
|
||||
* @param response
|
||||
*/
|
||||
public getGroupsForResponse(response: ISearchRequestResult): Array<GroupInfo> {
|
||||
const groups = new Array<GroupInfo>();
|
||||
for (const group of pResponse.data.search.groups) {
|
||||
for (const group of response.data.search.groups) {
|
||||
groups.push(new GroupInfo(group.id, group.name));
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
public buildJsonUser(name_: String): any {
|
||||
const body = {
|
||||
query: `query($name: String!) {
|
||||
search(query:$name, first: 100, offset: 0) {
|
||||
users{
|
||||
profilePicture,
|
||||
name,
|
||||
id,
|
||||
handle,
|
||||
points,
|
||||
level,
|
||||
friends {
|
||||
id
|
||||
}
|
||||
}
|
||||
groups{
|
||||
id
|
||||
name
|
||||
creator{id name handle}
|
||||
members{id name handle}
|
||||
}
|
||||
}
|
||||
}`
|
||||
, variables: {
|
||||
name: name_
|
||||
}
|
||||
};
|
||||
return body;
|
||||
/**
|
||||
* Searches for users, groups, events and posts with a specified query.
|
||||
* @param query
|
||||
*/
|
||||
public search(query: string): Observable<ISearchRequestResult> {
|
||||
const body = SearchService.buildRequestBody(query);
|
||||
return this.http.post<ISearchRequestResult>(environment.graphQLUrl, body, {headers: this.headers});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue