Skip to content

Commit

Permalink
Add room operations
Browse files Browse the repository at this point in the history
  • Loading branch information
adelifar committed Mar 2, 2019
1 parent 3043bab commit 485615e
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 23 deletions.
53 changes: 51 additions & 2 deletions backend/routes/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const router = express.Router();
const verifyToken = require('../authenticate/key-check');
const adminCheck = require('../authenticate/admin-check');
const roomModel = require('../model/room');
router.delete('/:id', verifyToken, adminCheck, (erq, res, next) => {
router.delete('/:id', verifyToken, adminCheck, (req, res, next) => {
let id = req.params.id;
mongoose.connect(config.dbAddress, er => {
if (er) throw er;
let changedId = mongoose.Types.ObjectId(id);
roomModel.remove({_id: changedId}, (err, data) => {
roomModel.deleteOne({_id: changedId}, (err, data) => {
if (err) throw err;
res.send(data);
})
Expand All @@ -25,5 +25,54 @@ router.get('/adminList', verifyToken, adminCheck, (req, res, next) => {
})
});
});
router.get('/:id', verifyToken, adminCheck, (req, res, next) => {
let id = req.params.id;
mongoose.connect(config.dbAddress, er => {
if (er) throw er;
let changedId = mongoose.Types.ObjectId(id);
roomModel.findOne({_id: changedId}, (err, data) => {
if (err) throw err;
res.send(data);
})
})
});
router.post('/:id', verifyToken, adminCheck, (req, res, next) => {
let id = req.params.id;
mongoose.connect(config.dbAddress, er => {
if (er) throw er;
let changedId = mongoose.Types.ObjectId(id);
roomModel.findOne({_id: changedId}, (err, data) => {
if (err) throw err;
data.name = req.body.name;
data.save((error, data) => {
if (error) throw error;
res.send(data);
})
})
})
}
);
router.put('', verifyToken, adminCheck, (req, res, next) => {
mongoose.connect(config.dbAddress, er => {
if (er) throw er;
let r = roomModel({
name: req.body.name,
date: new Date(),

});
r.save((err, data) => {
if (err) throw err;
res.send(data);
})
})
});
router.get('', verifyToken, (req, res, next) => {
mongoose.connect(config.dbAddress, er => {
if (er) throw er;
roomModel.find({}, (err, data) => {
if (err) throw err;
res.send(data);
})
});
});
module.exports = router;
29 changes: 26 additions & 3 deletions src/app/admin/room-edit/room-edit.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
<p>
room-edit works!
</p>
<div *ngIf="error">
<h4 class="alert-danger">{{error}}</h4>
</div>
<div class="card">
<div class="card-header">
{{id!='-1'?'Edit Room':'Add Room'}}
</div>
<div class="card-body">
<div class="row">

<div class="col-md-8">
<table class="table table-responsive table-responsive-lg">
<tr>
<td>Name*:</td>
<td><input class="form-control" [(ngModel)]="selectedRoom.name"></td>
</tr>

</table>
<div class="row">
<button (click)="getBack()" class="btn btn-secondary m-3">Back</button>
<button (click)="updateRoom()" class="btn btn-secondary m-3">{{id=='-1'?'Add':'Update'}}</button>
</div>
</div>
</div>
</div>
</div>
49 changes: 47 additions & 2 deletions src/app/admin/room-edit/room-edit.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
import { Component, OnInit } from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {Room} from "../../../Room";
import {BackendService} from "../../backend.service";
import {ActivatedRoute, Router} from "@angular/router";
import {Location} from '@angular/common';

@Component({
selector: 'app-room-edit',
templateUrl: './room-edit.component.html',
styleUrls: ['./room-edit.component.css']
})
export class RoomEditComponent implements OnInit {
resetVar = false;
selectedRoom: Room;
id = '-1';
error: string;
private login: string;

constructor() { }
constructor(private location: Location, private bService: BackendService, private router: Router,private route:ActivatedRoute) {
}

getRoom() {
const routeId = this.route.snapshot.paramMap.get('id');
if (routeId != null) {
this.id = routeId;
this.bService.getRoom(this.id).subscribe(v => this.selectedRoom = v);
} else {
this.selectedRoom = new Room();
}
}

getLoginName() {
this.login = localStorage.getItem('name');
}

ngOnInit() {
this.getRoom();
this.getLoginName();
}

getBack() {
this.location.back();
}

updateRoom() {
this.error = undefined;
if (!this.selectedRoom.name) {
this.error = 'Please Enter the name of Room!';
return;
}
if (this.selectedRoom._id != null) {
this.bService.updateRoom(this.selectedRoom).subscribe(a => {
this.router.navigate(['rooms']);
}, er => this.router.navigate(['login']));
} else {
this.bService.addRoom(this.selectedRoom).subscribe(a => this.router.navigate(['rooms']), er => this.router.navigate(['login']));
}
}

}
43 changes: 29 additions & 14 deletions src/app/backend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ export class BackendService {
apiAddress = 'http://localhost:3000/api';
data: any;

getRooms(): Observable<Room[]> {
// return of(this.videos);

return this.http.get<Room[]>(this.apiAddress + '/room', httpOptions);
}

getCustomersForReserve(): Observable<Customer[]> {
return this.http.get<Customer[]>(this.apiAddress + '/customer/get', httpOptions);
Expand Down Expand Up @@ -91,26 +87,45 @@ export class BackendService {
reserveVideo(vid: Video) {
return this.http.post(`${this.apiAddress}/video/reserve/${vid._id}`, vid, httpOptions);
}
getRooms(): Observable<Room[]> {

register(loginModel: LoginModel) {
return this.http.post(this.apiAddress + '/auth/register', loginModel, httpOptions);
return this.http.get<Room[]>(this.apiAddress + '/room', this.getHttpOptions());
}

deleteRoom(_id: string) {
private getHttpOptions() {
const item = localStorage.getItem('token') != null ? localStorage.getItem('token') : '';

const myHttpOptions = {
return {
headers: new HttpHeaders({'Content-Type': 'application/json', 'x-access-token': item})
};
return this.http.delete(`${this.apiAddress}/room/${_id}`, myHttpOptions);
}

register(loginModel: LoginModel) {
return this.http.post(this.apiAddress + '/auth/register', loginModel, httpOptions);
}

deleteRoom(_id: string) {

return this.http.delete(`${this.apiAddress}/room/${_id}`, this.getHttpOptions());
}

getAdminRooms() {
const item = localStorage.getItem('token') != null ? localStorage.getItem('token') : '';

const myHttpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json', 'x-access-token': item})
};
return this.http.get<Room[]>(this.apiAddress + '/room/adminList', myHttpOptions);
return this.http.get<Room[]>(this.apiAddress + '/room/adminList', this.getHttpOptions());
}

getRoom(id: string) {

return this.http.get<Room>(`${this.apiAddress}/room/${id}`, this.getHttpOptions());
}

updateRoom(selectedRoom: Room) {

return this.http.post(`${this.apiAddress}/room/${selectedRoom._id}`,selectedRoom, this.getHttpOptions());
}

addRoom(selectedRoom: Room) {

return this.http.put(this.apiAddress + '/room', JSON.stringify(selectedRoom), this.getHttpOptions());
}
}
1 change: 1 addition & 0 deletions src/app/list/list.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<nav class=" navbar navbar-light bg-light">
<span>Room List</span>
<span>{{login}}</span>
<button (click)="logout()" class="btn btn-primary float-right">Logout</button>
</nav>
<hr>
Expand Down
9 changes: 8 additions & 1 deletion src/app/list/list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Router} from '@angular/router';
})
export class ListComponent implements OnInit {
list: Room[];
login: string;
@Input() searchQuery = '';

constructor(private httpService: BackendService, private router: Router) {
Expand All @@ -28,11 +29,17 @@ export class ListComponent implements OnInit {
getRooms() {
this.httpService.getRooms().subscribe(roomList => {
this.list = roomList;
});
}, er => this.router.navigate(['login']));
}

getLoginName() {
let item = localStorage.getItem('name');
this.login = item;
}

ngOnInit() {
this.getRooms();
this.getLoginName();
}

}
2 changes: 1 addition & 1 deletion src/app/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class LoginComponent implements OnInit {
} else {
this.data = res;
if (!this.data.auth) {
this.error = this.data.msg;
this.error = this.data.errorMsg;
this.login = '';
this.password = '';
return;
Expand Down
4 changes: 4 additions & 0 deletions src/app/register/register.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ <h4 class="alert-danger">{{error}}</h4>
<label for="password">Password</label>
<input type="password" class="form-control" id="password" placeholder="Password" [(ngModel)]="password">
</div>
<div class="form-group">
<label for="passwordConfirm">Confirm Password</label>
<input type="password" class="form-control" id="passwordConfirm" placeholder="Password" [(ngModel)]="passwordConfirm">
</div>
<button (click)="register()" class="btn btn-primary ">Register Me</button>
<a href="/login" class="btn btn-secondary m-3">Login</a>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/app/register/register.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {LoginModel} from '../../loginModel';
export class RegisterComponent implements OnInit {
login: string;
password: string;
passwordConfirm: string;
error: string;
data: any;

Expand All @@ -25,6 +26,10 @@ export class RegisterComponent implements OnInit {
this.error = 'Enter both username and password field';
return;
}
if (this.password !== this.passwordConfirm) {
this.error='Password and Password confirm not match';
return;
}
const loginModel: LoginModel = {login: this.login, password: this.password, _id: ''};
this.bService.register(loginModel).subscribe(res => {
if (!res) {
Expand Down

0 comments on commit 485615e

Please sign in to comment.