Hey there, I am a newcomer to both Angular and NestJS. I'm currently facing an issue with updating a specific row in my database through the frontend. While I can easily insert new data, updating seems to be problematic. Here's how my files are structured:
Front-End
update.component.html
<div>
<form class="form">
<div class="pageTitle title"> Update Structure/Department Information </div>
<div class="secondaryTitle title">Insert Department Name you want to update</div>
<input type="text" class="name formEntry" placeholder="Department Name" name="something" [(ngModel)] = "MyDept"/>
<button class="get formEntry" (click)="getManager()">Get</button>
<ul class="flight_headers" *ngFor="let org of barg">
<li class="departs cell"> Description: <input type="text" name="DN" [(ngModel)]="org.description"/> </li><br/>
<li class="arrives cell"> managing department: <input type="text" name="bDN" [(ngModel)]="org.managing_department"/> </li> <br/>
<button class="get formEntry" (click)="update(org)">Update</button>
</ul>
update.component.ts
import { Component, OnInit } from '@angular/core';
import { OrganiService } from '../organi.service';
import { Organi } from '../organi.model';
@Component({
selector: 'app-update',
templateUrl: './update.component.html',
styleUrls: ['./update.component.scss']
})
export class UpdateComponent implements OnInit {
constructor(private organiService: OrganiService) { }
barg: Organi[];
ngOnInit(): void {
}
getManager(): void{
let name:string = this.MyDept;
this.organiService.getManagingStructures(name).subscribe(data=>{
this.barg = data;
})
}
update(org: Organi): void{
this.organiService.updateStructure(org);
window.location.reload()
}
}
organi.service.ts
import { Injectable, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient} from '@angular/common/http';
import { Organi } from './organi.model';
@Injectable({
providedIn: 'root'
})
export class OrganiService {
constructor(private http: HttpClient) {
}
getManagingStructures(name: string): Observable<any> {
return this.http.get('http://localhost:3000/structures/query/'+name);
}
getSubordinateStructures(name: string): Observable<any> {
return this.http.get('http://localhost:3000/structures/query2/'+name);
}
postStructure(org: Organi) {
return this.http.post('http://localhost:3000/structures',org).subscribe(data =>{
console.log("New Structure Created!")
})
}
updateStructure(myData: Organi) {
return this.http.post('http://localhost:3000/structures/update',myData);
}
deleteStructure(id: number) {
}
}
Back-end
structures.controller.ts
import { Controller, Get, Post, Param, Body, Put, Delete, Patch } from '@nestjs/common';
import { StructuresService } from './structures.service';
import { Structure } from './structure.model';
import { Organization } from './structure.entity';
@Controller('structures')
export class StructuresController {
constructor(private readonly structuresService: StructuresService) {}
@Get()
findAll() {
return this.structuresService.findAll();
}
@Get("query/:name")
async query(@Param('name') name): Promise<any> {
return this.structuresService.query(name);
}
@Get("query2/:boss")
async query2(@Param('boss') boss): Promise<any> {
return this.structuresService.query2(boss);
}
@Post()
async create(@Body() structure: Structure): Promise<Organization[]> {
return this.structuresService.create(structure);
}
@Patch("update")
async update(@Body() structure: Structure): Promise<any> {
return this.structuresService.update(structure);
}
/* @Delete(':id')
remove(@Param('id') id: string) {
return this.structuresService.remove(+id);
} */
}
structures.services.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, UpdateResult } from 'typeorm';
import { Organization } from './structure.entity';
import { Structure } from './structure.model';
//import { structure } from './structure.model'
@Injectable()
export class StructuresService {
constructor(
@InjectRepository(Organization)
private readonly structureRepository: Repository<Organization>,
){}
async findAll(): Promise<any> {
return this.structureRepository.find();
}
async findManager(CEO: string): Promise<any> {
return this.structureRepository.find();
}
async query(Myname: string): Promise<any> {
return await this.structureRepository.find({name: Myname});
}
async query2(boss: string): Promise<any> {
return await this.structureRepository.find({managing_department: boss});
}
async create(structure: Structure): Promise<any> {
return await this.structureRepository.save(structure);
}
async update(structure: Structure): Promise<UpdateResult> {
return await this.structureRepository.update(structure.id, structure);
}
}