I'm coding a Ticketsystem right now, and now im at this point where I have a page to Display the Ticket and I need to add the opportunity to answer the Ticket over and over again. I am able to edit a Ticket with a put() but then im Overwriting the old Content. So I need a way to add new Text without deleting the older one. Edit: There are 2 Users, TicketCreator and then someone who answers.
Im using Angular, NestJS and MongoDB (via TypeORM)
Im going to provide my ticketdetail.component.html & ticketdetail.component.ts (from Angular) and my ticket.service.ts & ticket.controller.ts to show you what I got.
ticketdetail.component.html:
<div class="container">
<div>
<div>
<h2>
<span class="badge badge-flenders">Ticket von {{ data.creator }} </span>
</h2>
<p *ngIf="data.state === 'ungelesen'" class="badge badge-danger">
{{ data.id }}
</p>
<p *ngIf="data.state != 'ungelesen'" class="badge badge-success">
{{ data.id }}
</p>
</div>
<div class="boxed">
<span> {{ data.content }} </span>
</div>
<button type="button" class="btn btn-primary mb-4 mt-0.5">Antworten</button>
</div>
</div>
ticketdetail.component.ts:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ticketDto } from 'src/app/services/dtos/ticket.dto';
import { userDto } from 'src/app/services/dtos/user.dto';
import { TicketService } from 'src/app/services/ticketService.service';
@Component({
selector: 'app-ticketdetail',
templateUrl: './ticketdetail.component.html',
styleUrls: ['./ticketdetail.component.scss'],
})
export class TicketdetailComponent implements OnInit {
ticketId: any;
data!: ticketDto;
constructor(
private route: ActivatedRoute,
private ticketService: TicketService
) {
this.route.params.subscribe((params: { [x: string]: any }) => {
this.ticketId = params['id'];
});
}
ngOnInit(): void {
this.ticketService
.getTicketById(this.ticketId)
.subscribe((ticket: ticketDto) => {
this.data = ticket;
});
}
}
ticket.service.ts:
import {
BadRequestException,
Injectable,
NotFoundException,
Param,
} from '@nestjs/common';
import { Observable, from } from 'rxjs';
import { CreateTicketDto } from '../dto/create-ticket.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Db, getConnection, ObjectID, Repository, UpdateResult } from 'typeorm';
import { TicketEntity } from '../entities/ticket.entity';
import { Iticket } from '../interfaces/ticket.interface';
import { MongoRepository } from 'typeorm';
@Injectable()
export class TicketService {
constructor(
@InjectRepository(TicketEntity)
private readonly ticketRepository: MongoRepository<TicketEntity>,
) {}
async createTicket(_ticket: Iticket): Promise<any> {
return this.ticketRepository.save(_ticket);
}
async findAll(): Promise<TicketEntity[]> {
return this.ticketRepository.find();
}
async findByCreator(ticketcreator: string): Promise<TicketEntity> {
console.log(ticketcreator);
return this.ticketRepository.findOne(ticketcreator);
}
async findById(id: string): Promise<TicketEntity> {
console.log(id);
return await this.ticketRepository.findOne(id);
}
updateTicket(id: ObjectID, ticket: Iticket): Observable<UpdateResult> {
return from(this.ticketRepository.update(id, ticket));
}
async deleteTicket(_id: ObjectID): Promise<string> {
const exists =
/* ObjectID.isValid(_id) && */ await this.ticketRepository.findOne(_id);
if (!exists) {
throw new NotFoundException();
}
const _result = await this.ticketRepository.delete(_id);
if (_result.affected == 1) {
return 'Erfolgreich gelöscht';
} else {
throw new BadRequestException();
}
}
async update(content: any): Promise<any> {
return await this.ticketRepository.update(content, content);
}
}
ticket.controller.ts:
import {
Body,
Controller,
Delete,
Get,
Logger,
Param,
Post,
Put,
} from '@nestjs/common';
import { TicketService } from '../services/ticket.service';
import { NotFoundException } from '@nestjs/common/exceptions/not-found.exception';
import { TicketEntity } from '../entities/ticket.entity';
import { lastValueFrom, firstValueFrom } from 'rxjs';
import { Observable } from 'rxjs';
import { CreateTicketDto } from '../dto/create-ticket.dto';
import { ApiOperation, ApiProperty } from '@nestjs/swagger';
import { ObjectID, UpdateDateColumn, UpdateResult } from 'typeorm';
import { Iticket } from '../interfaces/ticket.interface';
@Controller('tickets')
export class TicketController {
constructor(private readonly ticketService: TicketService) {}
@ApiOperation({
summary: 'Erstellen eines neuen Tickets.',
})
@Post(':create')
createTicket(@Body() ticket: CreateTicketDto): Promise<any> {
return this.ticketService.createTicket(ticket);
}
@ApiOperation({
summary: 'Bearbeiten eines Tickets.',
})
@Post(':id')
async insertTicket() {}
@ApiOperation({
summary: 'Auflistung aller vorhandenen Tickets',
})
@Get()
async findAll(): Promise<TicketEntity[]> {
return this.ticketService.findAll();
}
@ApiOperation({
summary: 'Anzeige eines bestimmten Tickets anhand des Erstellers.',
})
@Get('/:id')
async findOne(@Param('id') id: string) {
let _ticket = await this.ticketService.findById(id);
console.log(_ticket);
if (!_ticket) {
throw new NotFoundException('Error.');
}
return _ticket;
}
/*async findByCreator(ticketcreator: string): Promise<TicketEntity> {
return this.ticketService.findByCreator(ticketcreator);
} */
@Delete(':id')
async del(@Param('id') id: ObjectID) {
return this.ticketService.deleteTicket(id);
}
@Put(':id')
update(
@Param('id') id: ObjectID,
@Body() ticket: Iticket,
): Observable<UpdateResult> {
return this.ticketService.updateTicket(id, ticket);
}
}