Skip to content
Snippets Groups Projects
Commit 138d8c54 authored by Tormod Nygård's avatar Tormod Nygård
Browse files

Merge branch '18-se-en-bruker-sin-lokasjon' into 'master'

Resolve "Se en bruker sin lokasjon"

Closes #18

See merge request !30
parents 5cfac964 0676a191
No related branches found
No related tags found
1 merge request!30Resolve "Se en bruker sin lokasjon"
Pipeline #126025 passed
Showing
with 64 additions and 19 deletions
...@@ -8,6 +8,7 @@ export class User implements Deserializable, Serializable { ...@@ -8,6 +8,7 @@ export class User implements Deserializable, Serializable {
private password: string; private password: string;
private create_time: Date; private create_time: Date;
private isAdmin: number; private isAdmin: number;
private location: string;
constructor(input: any = null) { constructor(input: any = null) {
if (input) { if (input) {
...@@ -19,6 +20,7 @@ export class User implements Deserializable, Serializable { ...@@ -19,6 +20,7 @@ export class User implements Deserializable, Serializable {
this.password = null; this.password = null;
this.create_time = new Date(); this.create_time = new Date();
this.isAdmin = 0; this.isAdmin = 0;
this.location = null;
} }
} }
...@@ -26,7 +28,7 @@ export class User implements Deserializable, Serializable { ...@@ -26,7 +28,7 @@ export class User implements Deserializable, Serializable {
Object.assign(this, input); Object.assign(this, input);
return this; return this;
} }
serialize(): Object { serialize(): Object {
return { return {
userId: this.userId, userId: this.userId,
...@@ -35,6 +37,7 @@ export class User implements Deserializable, Serializable { ...@@ -35,6 +37,7 @@ export class User implements Deserializable, Serializable {
password: this.password, password: this.password,
create_time: this.create_time, create_time: this.create_time,
isAdmin: this.isAdmin, isAdmin: this.isAdmin,
location: this.location
}; };
} }
...@@ -83,6 +86,14 @@ export class User implements Deserializable, Serializable { ...@@ -83,6 +86,14 @@ export class User implements Deserializable, Serializable {
} }
set setIsAdmin(isAdmin: number) { set setIsAdmin(isAdmin: number) {
isAdmin = this.isAdmin; this.isAdmin = isAdmin;
}
get getLocation() {
return this.location;
}
set setLocation(location: string){
this.location = location;
} }
} }
\ No newline at end of file
...@@ -20,9 +20,7 @@ ...@@ -20,9 +20,7 @@
</div> </div>
<div class="ownerInfo"> <div class="ownerInfo">
<a [href]="'/user/'+owner.getUserId"> <u id="owner" (click)="navigateOwner()">{{owner.getUsername}}</u>
<p>Owner: {{owner.getUsername}}</p>
</a>
<p>E-mail: {{owner.getEmail}}</p> <p>E-mail: {{owner.getEmail}}</p>
</div> </div>
......
...@@ -47,6 +47,12 @@ div.container{ ...@@ -47,6 +47,12 @@ div.container{
box-shadow: inset 0px 4px 4px rgba(0, 0, 0, 0.5); box-shadow: inset 0px 4px 4px rgba(0, 0, 0, 0.5);
padding: 20px; padding: 20px;
u#owner{
color: #222222;
cursor: pointer;
font-size: 20pt;
}
div.pinkBox{ div.pinkBox{
background-color: #FFA1A1; background-color: #FFA1A1;
padding: 15px; padding: 15px;
......
...@@ -23,12 +23,12 @@ export class PostDetailsComponent implements OnInit { ...@@ -23,12 +23,12 @@ export class PostDetailsComponent implements OnInit {
soldToUser: number = 0; soldToUser: number = 0;
constructor(private postService: PostService, private activatedRoute: ActivatedRoute, private router: Router, constructor(private postService: PostService, private activatedRoute: ActivatedRoute, private router: Router,
private authService: AuthService, private userService: UserService) { } private authService: AuthService, private userService: UserService) { }
ngOnInit() { ngOnInit() {
// Gets current user information // Gets current user information
this.user = this.authService.getCurrentUser(false); this.user = this.authService.getCurrentUser(false);
// If user is logged in, assign userId and isAdmin // If user is logged in, assign userId and isAdmin
this.userId = this.user.getUserId; this.userId = this.user.getUserId;
this.isAdmin = this.user.getIsAdmin; this.isAdmin = this.user.getIsAdmin;
...@@ -50,7 +50,14 @@ export class PostDetailsComponent implements OnInit { ...@@ -50,7 +50,14 @@ export class PostDetailsComponent implements OnInit {
console.log(error); console.log(error);
}); });
} }
/**
* Navigates to owner's profile
*/
navigateOwner() {
this.router.navigateByUrl("/user/" + this.owner.getUserId);
}
/** /**
* Moves to edit page * Moves to edit page
*/ */
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<div class="titleWrapper"> <div class="titleWrapper">
<div class="img"></div> <div class="img"></div>
<div class="info"> <div class="info">
<p class="name">{{user.getUsername}} Doe</p> <p class="name">{{user.getUsername}}</p>
<p class="email">{{user.getEmail}}</p> <p class="email">{{user.getEmail}}</p>
<p class="phone_number">+47 123 45 678</p> <p class="phone_number">+47 123 45 678</p>
</div> </div>
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
<i class="material-icons">star_border</i> <i class="material-icons">star_border</i>
</div> </div>
<div class="location"> <div class="location">
Geografi: Trondheim, Oslo Område: {{user.getLocation}}
</div> </div>
<div class="description"> <div class="description">
<pre> <pre>
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
<div class="titleWrapper"> <div class="titleWrapper">
<div class="img"></div> <div class="img"></div>
<div class="info"> <div class="info">
<p class="name">{{user.getUsername}} Doe</p> <p class="name">{{user.getUsername}}</p>
<p class="email">{{user.getEmail}}</p> <p class="email">{{user.getEmail}}</p>
<p class="phone_number">+47 123 45 678</p> <p class="phone_number">+47 123 45 678</p>
</div> </div>
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
<i class="material-icons">star_border</i> <i class="material-icons">star_border</i>
</div> </div>
<div class="location"> <div class="location">
Geografi: Trondheim, Oslo Område: {{user.getLocation}}
</div> </div>
<div class="description"> <div class="description">
<pre> <pre>
......
...@@ -9,6 +9,20 @@ ...@@ -9,6 +9,20 @@
<app-input [(inputModel)]="username" label="Brukernavn" (blur)="checkForm()"></app-input> <app-input [(inputModel)]="username" label="Brukernavn" (blur)="checkForm()"></app-input>
<app-input [(inputModel)]="email" label="Epost" (blur)="checkForm()"></app-input> <app-input [(inputModel)]="email" label="Epost" (blur)="checkForm()"></app-input>
<app-input [(inputModel)]="phone_number" label="Mobilnummer" (blur)="checkForm()"></app-input> <app-input [(inputModel)]="phone_number" label="Mobilnummer" (blur)="checkForm()"></app-input>
<app-select [(inputModel)]="location">
<option selected>Velg fylke . . .</option>
<option>Agder</option>
<option>Innlandet</option>
<option>Møre og Romsdal</option>
<option>Nordland</option>
<option>Oslo</option>
<option>Rogaland</option>
<option>Troms og Finnmark</option>
<option>Trøndelag</option>
<option>Vestfold og Telemark</option>
<option>Vestland</option>
<option>Viken</option>
</app-select>
<app-input [(inputModel)]="password" type="password" label="Passord" (blur)="checkForm()"></app-input> <app-input [(inputModel)]="password" type="password" label="Passord" (blur)="checkForm()"></app-input>
<app-input [(inputModel)]="confirm_password" type="password" label="Bekreft passord" (blur)="checkForm()"></app-input> <app-input [(inputModel)]="confirm_password" type="password" label="Bekreft passord" (blur)="checkForm()"></app-input>
<p class="status">{{statusMessage}}</p> <p class="status">{{statusMessage}}</p>
......
...@@ -15,6 +15,7 @@ export class UserRegistrationFormComponent implements OnInit { ...@@ -15,6 +15,7 @@ export class UserRegistrationFormComponent implements OnInit {
username: string = ""; username: string = "";
email: string = ""; email: string = "";
phone_number: string = ""; phone_number: string = "";
location: string = "Velg fylke . . .";
password: string = ""; password: string = "";
confirm_password: string = ""; confirm_password: string = "";
...@@ -49,6 +50,10 @@ export class UserRegistrationFormComponent implements OnInit { ...@@ -49,6 +50,10 @@ export class UserRegistrationFormComponent implements OnInit {
this.setStatusMessage("Mobilnummer kan ikke være tom"); this.setStatusMessage("Mobilnummer kan ikke være tom");
return false; return false;
} }
else if (this.location == "Velg fylke . . .") {
this.setStatusMessage("Fylke må være valgt");
return false;
}
else if (this.password == "") { else if (this.password == "") {
this.setStatusMessage("Passordet kan ikke være tom"); this.setStatusMessage("Passordet kan ikke være tom");
return false; return false;
...@@ -76,6 +81,7 @@ export class UserRegistrationFormComponent implements OnInit { ...@@ -76,6 +81,7 @@ export class UserRegistrationFormComponent implements OnInit {
email: this.email, email: this.email,
password: this.password, password: this.password,
isAdmin: 0, isAdmin: 0,
location: this.location
}); });
// Adds user to database and redirects to the homepage afterwards // Adds user to database and redirects to the homepage afterwards
......
...@@ -10,7 +10,7 @@ const router = express.Router(); ...@@ -10,7 +10,7 @@ const router = express.Router();
// Post register user `/api/auth/register` // Post register user `/api/auth/register`
router.route('/register').post(async (request: Request, response: Response) => { router.route('/register').post(async (request: Request, response: Response) => {
const {username, email, password, isAdmin, create_time} = request.body; const {username, email, password, isAdmin, location, create_time} = request.body;
try { try {
// Check valid request data parameters // Check valid request data parameters
const user_data: IUser = { const user_data: IUser = {
...@@ -18,6 +18,7 @@ router.route('/register').post(async (request: Request, response: Response) => { ...@@ -18,6 +18,7 @@ router.route('/register').post(async (request: Request, response: Response) => {
"email": email, "email": email,
"password": password, "password": password,
"isAdmin": isAdmin || 0, "isAdmin": isAdmin || 0,
"location": location || null
}; };
if (Object.values(user_data).filter(p => p == undefined).length > 0) return response.status(500).send("Error"); if (Object.values(user_data).filter(p => p == undefined).length > 0) return response.status(500).send("Error");
// Check for user duplicates // Check for user duplicates
...@@ -28,7 +29,7 @@ router.route('/register').post(async (request: Request, response: Response) => { ...@@ -28,7 +29,7 @@ router.route('/register').post(async (request: Request, response: Response) => {
return response.status(403).send("There exists an user with the same username or emails given!"); return response.status(403).send("There exists an user with the same username or emails given!");
} }
// If there is no duplicates, create new user // If there is no duplicates, create new user
const input = (`INSERT INTO user(username, email, password, isAdmin) VALUES (?,?,?,?)`) const input = (`INSERT INTO user(username, email, password, isAdmin, location) VALUES (?,?,?,?,?)`)
return response.status(200).json( return response.status(200).json(
await query(input,Object.values(user_data)) await query(input,Object.values(user_data))
); );
...@@ -41,7 +42,7 @@ router.route('/register').post(async (request: Request, response: Response) => { ...@@ -41,7 +42,7 @@ router.route('/register').post(async (request: Request, response: Response) => {
router.route('/login').post(async (request: Request, response: Response) => { router.route('/login').post(async (request: Request, response: Response) => {
const {username, password} = request.body; const {username, password} = request.body;
try { try {
const input = "SELECT userId, username, email, isAdmin, create_time FROM user WHERE username=? AND password=?;" const input = "SELECT userId, username, email, isAdmin, create_time, location FROM user WHERE username=? AND password=?;"
const user = await query(input,[username, password]); const user = await query(input,[username, password]);
// Check if an user object is retrieved // Check if an user object is retrieved
const userObj = Object.values(JSON.parse(JSON.stringify(user.data)))[0]; const userObj = Object.values(JSON.parse(JSON.stringify(user.data)))[0];
......
...@@ -8,18 +8,19 @@ const router = express.Router(); ...@@ -8,18 +8,19 @@ const router = express.Router();
/* ============================= CREATE ============================= */ /* ============================= CREATE ============================= */
// Create an user `/api/user/` // Create an user `/api/user/`
router.route('/').post(async (request: Request, response: Response) => { router.route('/').post(async (request: Request, response: Response) => {
const {username, email, password, isAdmin, create_time} = request.body; // destructuring const {username, email, password, isAdmin, location, create_time} = request.body; // destructuring
try { try {
const user: IUser = { const user: IUser = {
"username": username, "username": username,
"email": email, "email": email,
"password": password, "password": password,
"isAdmin": isAdmin || 0, "isAdmin": isAdmin || 0,
"location": location || null
}; };
if (Object.values(user).filter(p => p == undefined).length > 0) return response.status(500).send("Error"); if (Object.values(user).filter(p => p == undefined).length > 0) return response.status(500).send("Error");
const input = (`INSERT INTO user(username, email, password, isAdmin) VALUES (?,?,?,?)`); const input = (`INSERT INTO user(username, email, password, isAdmin, location) VALUES (?,?,?,?,?)`);
return response.status(200).json( return response.status(200).json(
await query(input,Object.values(user)) await query(input, Object.values(user))
); );
} catch (error) { } catch (error) {
return response.status(400).send("Bad Request"); return response.status(400).send("Bad Request");
...@@ -40,7 +41,7 @@ router.route('/').get(async (_: Request, response: Response) => { ...@@ -40,7 +41,7 @@ router.route('/').get(async (_: Request, response: Response) => {
router.route('/:userId').get(async (request: Request, response: Response) => { router.route('/:userId').get(async (request: Request, response: Response) => {
const userId = request.params.userId; const userId = request.params.userId;
try { try {
const input = `SELECT userId, username, email, create_time FROM user WHERE userId=?;` const input = `SELECT userId, username, email, create_time, location FROM user WHERE userId=?;`
response.status(200).json(await query(input,[userId])); response.status(200).json(await query(input,[userId]));
} catch (error) { } catch (error) {
response.status(400).send("Bad Request"); response.status(400).send("Bad Request");
......
...@@ -6,6 +6,7 @@ interface IUser{ ...@@ -6,6 +6,7 @@ interface IUser{
password: string; password: string;
create_time?: Date; create_time?: Date;
isAdmin: number; isAdmin: number;
location: string;
} }
export default IUser; export default IUser;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment