Commit ca671bbf by Harangozó Péter

users &rights crud

parent 14bca496
......@@ -52,6 +52,10 @@
},
"defaults": {
"styleExt": "less",
"component": {}
"component": {},
"serve":{
"host": "192.168.50.183",
"port": 8080
}
}
}
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { UsersComponent } from './modules/users/users.component';
import { UserComponent } from './modules/users/user/user.component';
import { RightsComponent } from './modules/rights/rights.component';
import { RightComponent } from './modules/rights/right/right.component';
const routes: Routes = [
{
path: '',
children: []
},
//Users
{
path: 'users',
component: UsersComponent
},
{
path: 'users/edit/:id',
component: UserComponent
},
{
path: 'users/create',
component: UserComponent
},
{
path: 'users/details/:id',
component: UserComponent
},
//Rights
{
path: 'rights',
component: RightsComponent
},
{
path: 'rights/edit/:id',
component: RightComponent
},
{
path: 'rights/create',
component: RightComponent
},
{
path: 'rights/details/:id',
component: RightComponent
}
];
@NgModule({
......
......@@ -8,6 +8,7 @@
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" routerLink="" routerLinkActive="active">Home</a></li>
<li class="nav-item"><a class="nav-link" routerLink="users" routerLinkActive="active">Users</a></li>
<li class="nav-item"><a class="nav-link" routerLink="rights" routerLinkActive="active">Rights</a></li>
</ul>
</div>
</nav>
......
......@@ -8,19 +8,29 @@ import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { UsersComponent } from './modules/users/users.component';
import { UserService } from './modules/users/services/user.service';
import { UserComponent } from './modules/users/user/user.component';
import { RightsComponent } from './modules/rights/rights.component';
import { RightComponent } from './modules/rights/right/right.component';
import { RightService } from './modules/rights/services/right.service';
@NgModule({
declarations: [
AppComponent,
UsersComponent,
UserComponent,
RightsComponent,
RightComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule
AppRoutingModule,
NgbModule.forRoot()
],
providers: [UserService],
providers: [UserService, RightService],
bootstrap: [AppComponent]
})
export class AppModule { }
export class Right {
Name: string;
}
<h2>Right - {{title}}</h2>
<hr>
<button type="button" class="btn btn-sm btn-outline-primary" (click)="goBack()">Back</button>
<button type="button" class="btn btn-sm btn-outline-success float-right" (click)="save()" [hidden]="!editMode">Save</button>
<hr>
<div *ngIf="editMode">
<div class="form-group row">
<label for="Name" class="col-2 col-form-label">Name</label>
<div class="col-10">
<input id="Name" class="form-control" type="text" [(ngModel)]="right.Name" placeholder="Name" />
</div>
</div>
</div>
<div *ngIf="!editMode">
<div class="form-group row">
<label for="Name" class="col-2 col-form-label">Name</label>
<div class="col-10">
<p class="form-control-static">
{{right.Name}}
</p>
</div>
</div>
</div>
\ No newline at end of file
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RightComponent } from './right.component';
describe('RightComponent', () => {
let component: RightComponent;
let fixture: ComponentFixture<RightComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ RightComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(RightComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';
import { Right } from '../models/right';
import { RightService } from '../services/right.service'
@Component({
selector: 'app-right',
templateUrl: './right.component.html',
styleUrls: ['./right.component.less']
})
export class RightComponent implements OnInit {
right: Right;
editMode: boolean;
title: string;
constructor(
private rightService: RightService,
private route: ActivatedRoute,
private location: Location,
) { }
ngOnInit(): void {
this.right = new Right();
this.editMode = true;
this.route.params.subscribe(params => {
if (params['id']) {
this.rightService.getRight(params['id'])
.then(right => {
this.right = right
});
}
if (this.location.path().indexOf('details') > -1) {
this.editMode = false;
this.title = "Details";
}
else {
if (params['id'])
this.title = "Edit";
else {
this.title = "Create";
this.right = new Right();
}
}
});
}
save(): void {
this.rightService.createRight(this.right)
.then(() => this.goBack());
}
goBack(): void {
this.location.back();
}
}
<h2>Rights</h2>
<hr>
<button type="button" class="btn btn-sm btn-outline-primary" routerLink="/rights/create">Add right</button>
<hr>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let right of rights">
<th scope="row">{{right.id}}</th>
<td>{{right.Name}}</td>
<td>
<button type="button" class="btn btn-sm btn-outline-info" (click)="details(right.id)">Details</button>
<button type="button" class="btn btn-sm btn-outline-warning" (click)="edit(right.id)">Edit</button>
<button type="button" class="btn btn-sm btn-outline-danger">Delete</button>
</td>
</tr>
</tbody>
</table>
\ No newline at end of file
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { RightsComponent } from './rights.component';
describe('RightsComponent', () => {
let component: RightsComponent;
let fixture: ComponentFixture<RightsComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ RightsComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(RightsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Right } from './models/right';
import { RightService } from './services/right.service';
@Component({
selector: 'app-rights',
templateUrl: './rights.component.html',
styleUrls: ['./rights.component.less']
})
export class RightsComponent implements OnInit {
rights: Right[];
constructor(
private rightService: RightService,
private router: Router) { }
getRights(): void {
this.rightService
.getRights()
.then(rights => this.rights = rights);
}
ngOnInit() {
this.getRights();
}
details(id): void {
this.router.navigate(['/rights/details', id]);
}
edit(id): void {
this.router.navigate(['/rights/edit', id]);
}
}
import { TestBed, inject } from '@angular/core/testing';
import { RightService } from './right.service';
describe('RightService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [RightService]
});
});
it('should ...', inject([RightService], (service: RightService) => {
expect(service).toBeTruthy();
}));
});
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap';
import { Right } from '../models/right';
@Injectable()
export class RightService {
private headers = new Headers({ 'Content-Type': 'application/json' });
private rightsUrl = 'api/right'; // URL to web api
constructor(
private http: Http,
private parserFormatter: NgbDateParserFormatter
) { }
getRights(): Promise<Right[]> {
return this.http.get('http://192.168.50.183:1337/' + this.rightsUrl)
.toPromise()
.then(response =>
response.json().rights as Right[])
;
}
getRight(id: number): Promise<Right> {
const url = `${this.rightsUrl}/${id}`;
return this.http.get('http://192.168.50.183:1337/' + url)
.toPromise()
.then(response => response.json().right as Right);
}
createRight(right: Right): Promise<Right> {
return this.http
.post('http://192.168.50.183:1337/' + this.rightsUrl, JSON.stringify(right), { headers: this.headers })
.toPromise()
.then(res => res.json().right)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
export class User {
BirthDate: Date;
BirthDate: any;
FirstName: string;
LastName: string;
UserName: string;
......
import { Injectable } from '@angular/core';
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { User } from '../models/user';
import 'rxjs/add/operator/toPromise';
import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap';
import { User } from '../models/user';
@Injectable()
export class UserService {
private headers = new Headers({ 'Content-Type': 'application/json' });
private usersUrl = 'api/user'; // URL to web api
constructor(private http: Http) { }
constructor(
private http: Http,
private parserFormatter: NgbDateParserFormatter
) { }
getUsers(): Promise<User[]> {
return this.http.get('http://localhost:1337/' + this.usersUrl)
return this.http.get('http://192.168.50.183:1337/' + this.usersUrl)
.toPromise()
.then(response =>
.then(response =>
response.json().users as User[])
;
}
getUser(id: number): Promise<User> {
const url = `${this.usersUrl}/${id}`;
return this.http.get('http://192.168.50.183:1337/' + url)
.toPromise()
.then(response => {
let user = response.json().user as User;
user.BirthDate = this.parserFormatter.parse(user.BirthDate);
return user;
});
}
createUser(user: User): Promise<User> {
user.BirthDate = this.parserFormatter.format(user.BirthDate);
return this.http
.post('http://192.168.50.183:1337/' + this.usersUrl, JSON.stringify(user), { headers: this.headers })
.toPromise()
.then(res => res.json().user)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
<h2>User - {{title}}</h2>
<hr>
<button type="button" class="btn btn-sm btn-outline-primary" (click)="goBack()">Back</button>
<button type="button" class="btn btn-sm btn-outline-success float-right" (click)="save()" [hidden]="!editMode">Save</button>
<hr>
<div *ngIf="editMode">
<div class="form-group row">
<label for="first-name" class="col-2 col-form-label">Firstname</label>
<div class="col-10">
<input id="first-name" class="form-control" type="text" [(ngModel)]="user.FirstName" placeholder="Firstname" />
</div>
</div>
<div class="form-group row">
<label for="last-name" class="col-2 col-form-label">Lastname</label>
<div class="col-10">
<input id="last-name" class="form-control" type="text" [(ngModel)]="user.LastName" placeholder="Lastname" />
</div>
</div>
<div class="form-group row">
<label for="user-name" class="col-2 col-form-label">Username</label>
<div class="col-10">
<input id="user-name" class="form-control" type="text" [(ngModel)]="user.UserName" placeholder="Username" />
</div>
</div>
<div class="form-group row">
<label for="birthdate" class="col-2 col-form-label">Birthdate</label>
<div class="col-10 input-group">
<input class="form-control" placeholder="yyyy-mm-dd" name="dp" [(ngModel)]="user.BirthDate" ngbDatepicker #d="ngbDatepicker">
<div class="input-group-addon" (click)="d.toggle()">
<i class="fa fa-6 fa-calendar" aria-hidden="true" style="width: 1.2rem; height: 1rem; cursor: pointer;"></i>
</div>
</div>
</div>
</div>
<div *ngIf="!editMode">
<div class="form-group row">
<label for="first-name" class="col-2 col-form-label">Firstname</label>
<div class="col-10">
<p class="form-control-static">
{{user.FirstName}}
</p>
</div>
</div>
<div class="form-group row">
<label for="last-name" class="col-2 col-form-label">Lastname</label>
<div class="col-10">
<p class="form-control-static">
{{user.LastName}}
</p>
</div>
</div>
<div class="form-group row">
<label for="user-name" class="col-2 col-form-label">Username</label>
<div class="col-10">
<p class="form-control-static">
{{user.UserName}}
</p>
</div>
</div>
<div class="form-group row">
<label for="birthdate" class="col-2 col-form-label">Birthdate</label>
<div class="col-10 input-group">
<p class="form-control-static">
{{parserFormatter.format(user.BirthDate)}}
</p>
</div>
</div>
</div>
\ No newline at end of file
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { UserComponent } from './user.component';
describe('UserComponent', () => {
let component: UserComponent;
let fixture: ComponentFixture<UserComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ UserComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(UserComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common';
import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap';
import { User } from '../models/user';
import { UserService } from '../services/user.service'
@Component({
selector: 'app-user',
templateUrl: './user.component.html',
styleUrls: ['./user.component.less']
})
export class UserComponent implements OnInit {
user: User;
editMode: boolean;
title: string;
constructor(
private userService: UserService,
private route: ActivatedRoute,
private location: Location,
private parserFormatter: NgbDateParserFormatter
) { }
ngOnInit(): void {
this.user = new User();
this.editMode = true;
this.route.params.subscribe(params => {
if (params['id']) {
this.userService.getUser(params['id'])
.then(user => {
this.user = user
});
}
if (this.location.path().indexOf('details') > -1) {
this.editMode = false;
this.title = "Details";
}
else {
if (params['id'])
this.title = "Edit";
else {
this.title = "Create";
this.user = new User();
}
}
});
}
save(): void {
this.userService.createUser(this.user)
.then(() => this.goBack());
}
goBack(): void {
this.location.back();
}
}
<h2>Users</h2>
<hr>
<button type="button" class="btn btn-sm btn-outline-primary">Add User</button>
<button type="button" class="btn btn-sm btn-outline-primary" routerLink="/users/create">Add User</button>
<hr>
<table class="table table-striped">
<thead>
<tr>
......@@ -18,7 +19,8 @@
<td>{{user.LastName}}</td>
<td>{{user.UserName}}</td>
<td>
<button type="button" class="btn btn-sm btn-outline-warning">Edit</button>
<button type="button" class="btn btn-sm btn-outline-info" (click)="details(user.id)">Details</button>
<button type="button" class="btn btn-sm btn-outline-warning" (click)="edit(user.id)">Edit</button>
<button type="button" class="btn btn-sm btn-outline-danger">Delete</button>
</td>
</tr>
......
......@@ -3,6 +3,9 @@ import { Router } from '@angular/router';
import { User } from './models/user';
import { UserService } from './services/user.service';
import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'app-users',
......@@ -26,4 +29,12 @@ export class UsersComponent implements OnInit {
this.getUsers();
}
details(id): void {
this.router.navigate(['/users/details', id]);
}
edit(id): void {
this.router.navigate(['/users/edit', id]);
}
}
......@@ -10,6 +10,8 @@
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn"
crossorigin="anonymous"></script>
<script src="https://use.fontawesome.com/0e9cdd0b7e.js"></script>
<meta charset="utf-8">
<title>UsersDojoNg</title>
<base href="/">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment