user9847788
user9847788

Reputation: 2431

How to create an object to send via HTTP POST request using values of another object in angular app?

In my Angular app, I have a Job object & an Offer object.

Here are my Interfaces:

export interface IJob {
  id: number;
  title: string;
  description: string;
  employeeId?: number;
  managerId: string;
  imageUrl: string;
}

export interface IOffer {
  id: number;
  managerId: number;
  jobId: number;
  employeeId: number;
}

I'm displaying all the Job details like so:

<table">
    <thead>
        <tr>
            <th scope="col">Title</th>
            <th scope="col">Description</th>
            <th scope="col">Employee ID</th>
            <th scope="col">Manager ID</th>
            <th scope="col">Action</th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let job of jobs">
            <td>{{ job.title }}</td>
            <td>{{job.description}}</td>
            <td>{{job.employeeId}}</td>
            <td>{{job.managerId}}</td>
            <td>
                <button (click)="applyForJob(job)">Apply Now</button>
            </td>
        </tr>
    </tbody>
</table>

I want to use this applyForJob(job) method to create an Offer object for the associated job.

I have this method in my Offer Service at the moment, if that helps:

addOffer(offer: IOffer): Observable<IOffer> {
    return this.httpClient.post<IOffer>(this.baseUrl, offer, {
      headers: new HttpHeaders({
        'Content-Type': 'application/json'
      })
    })
      .pipe(catchError(this.handleError));
  }

Can someone please tell me how I can create an offer for a specific job with the above code?

Upvotes: 1

Views: 70

Answers (1)

Sudarshana Dayananda
Sudarshana Dayananda

Reputation: 5265

You can do it as follows.

applyForJob(job) {

     let offer = new IOffer();
     offer.id = job.id; // use unique id for this
     offer.managerId = job.managerId;
     offer.jobId = job.id;
     offer.employeeId = job.employeeId;

     myOfficeService.addOffer(offer).subscribe((res: any) => {
        console.log(res);
     });

}

Upvotes: 1

Related Questions