Skip to content

Commit

Permalink
feat add delete Todoinstitution functionality, protected by authGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
holdan-8 committed May 23, 2024
1 parent 1a2787f commit 4fd56ba
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,15 @@ <h4>Add Organization(s)</h4>

<input
type="submit"
value="Save Institution"
value="Save/Update Institution"
class="submit-btn"
[disabled]="!reactiveForm.valid"
/>

<button type="button" class="delete-btn" (click)="DeleteInst()">
Delete Whole Institution
</button>

</form>
</section>

Expand Down
19 changes: 19 additions & 0 deletions frontend/src/app/add-institution/add-institution.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,28 @@
transition: all 0.2s ease;
background: #888;
}

.form .submit-btn:hover {
background: #555;
}

.form .delete-btn {
height: 55px;
width: 100%;
color: #fff;
font-size: 1rem;
font-weight: 400;
margin-top: 30px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: all 0.2s ease;
background: #d82222;
}

.form .delete-btn:hover {
background: #da6565;
}
/*Responsive*/
@media screen and (max-width: 500px) {
.form .column {
Expand Down
48 changes: 34 additions & 14 deletions frontend/src/app/add-institution/add-institution.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ export class AddInstitutionComponent implements OnInit {
formdata: any = {};
reactiveForm: FormGroup;
dataSource: any;
displayedColumns: string[] = ['name_de','uuid', 'sector', 'shortname', 'ts','orgs', 'edit'];
displayedColumns: string[] = [
'name_de',
'uuid',
'sector',
'shortname',
'ts',
'orgs',
'edit',
];

constructor(private dataService: DataService) {}

ngOnInit() {
this.dataService.LoadTodoInstitutions().then(data => {
this.dataService.LoadTodoInstitutions().then((data) => {
this.dataSource = data;
});

Expand Down Expand Up @@ -51,9 +59,7 @@ export class AddInstitutionComponent implements OnInit {

async OnFormSubmitted() {
this.formdata = this.reactiveForm.value;
await this.dataService.createNewTodoInstitution(
this.formdata,
);
await this.dataService.createNewTodoInstitution(this.formdata);
this.reactiveForm.reset({
name_de: null,
shortNnme: null,
Expand Down Expand Up @@ -82,33 +88,47 @@ export class AddInstitutionComponent implements OnInit {
const controls = <FormArray>this.reactiveForm.get('orgs');
controls.removeAt(index);
}
editTodoInstitution(institution) {


editTodoInstitution(institution) {
// Add additional FormGroups for the organizations if there are more in the institution than in the form
const orgsControl = <FormArray>this.reactiveForm.get('orgs');
while (orgsControl.length < institution.orgs.length) {
this.AddOrg();
}

// Remove additional FormGroups for the organizations if there are more in the form than in the institution
while (orgsControl.length > institution.orgs.length) {
this.DeleteOrg(orgsControl.length - 1);
}
// write values from chosen institution to the form
// write values from chosen institution to the form
this.reactiveForm.patchValue({
name_de: institution.name_de,
shortname: institution.shortname,
uuid: institution.uuid,
sector: institution.sector,
ts: institution.ts,
orgs: institution.orgs.map(org => ({
orgs: institution.orgs.map((org) => ({
name: org.name,
ts_org: org.ts
}))
ts_org: org.ts,
})),
});
}



async DeleteInst() {
this.formdata = this.reactiveForm.value;
await this.dataService.DeleteTodoInstitution(this.formdata);
this.reactiveForm.reset({
name_de: null,
shortNnme: null,
uuid: null,
sector: null,
ts: null,
orgs: [
{
name: null,
ts_org: null,
},
],
});
}
}
16 changes: 16 additions & 0 deletions frontend/src/app/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ export class DataService {
.toPromise();
}

async DeleteTodoInstitution(institution) {
if (!institution ) {
throw new Error('Invalid institution object');
}
const token = this.tokenService.getAccessToken();
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
const options = {
headers: headers,
body: { institution }
};
return await this.http
.delete<TodoInstitution>(`${environment.api}api/institution`, options)
.toPromise();

}

async LoadTodoInstitutions() {
this.TodoInstitutions = await this.http
.get<TodoInstitution>(`${environment.api}api/institution`)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/nav/nav.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</button>
<button mat-button routerLink="/add-institution" *ngIf="isLoggedIn()">
<mat-icon>note_add</mat-icon>
<span>Add an Institution</span>
<span>Add/Edit an Institution</span>
</button>
<!-- <button mat-button routerLink="/visualization">
<mat-icon>pie_chart</mat-icon>
Expand Down
7 changes: 7 additions & 0 deletions oss-api/src/api/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Query,
UsePipes,
ValidationPipe,
Delete,
} from '@nestjs/common';
import { InstitutionQueryPipe } from 'src/institution-query.pipe';
import {
Expand Down Expand Up @@ -106,6 +107,12 @@ export class ApiController {
return this.mongoDbService.createNewTodoInstitution(institution);
}

@UseGuards(AuthGuard)
@Delete('institution')
async deleteInstitution(@Body('institution') institution: TodoInstitution) {
return this.mongoDbService.deleteTodoInstitution(institution);
}

@UseGuards(AuthGuard)
@Get('institution')
async findTodoInstitution() {
Expand Down
2 changes: 1 addition & 1 deletion oss-api/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AuthController } from './auth.controller';
JwtModule.register({
global: true,
secret: jwtConstants.secret,
signOptions: { expiresIn: '300s' },
signOptions: { expiresIn: '900s' },
}),
AddInstitutionModule,
],
Expand Down
7 changes: 7 additions & 0 deletions oss-api/src/mongo-db/mongo-db.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,13 @@ export class MongoDbService implements OnApplicationShutdown, OnModuleInit {
.replaceOne({ uuid: institution.uuid }, institution, { upsert: true });
}

async deleteTodoInstitution(institution: TodoInstitution) {
return await this.client
.db(this.database)
.collection<TodoInstitution>(Tables.todoInstitutions)
.deleteOne({ uuid: institution.uuid });
}

/**
* Count all users with the given condtions
* @param conditions The conditions to filter with
Expand Down

0 comments on commit 4fd56ba

Please sign in to comment.