|
| 1 | +import { Controller, Get, Body, Put, Post } from '@nestjs/common'; |
| 2 | +import { AdminSettingsService } from '../application/admin-settings.service'; |
| 3 | +import { AdminSettings } from '../domain/admin-settings.entity'; |
| 4 | +import { UpdateAdminSettingsDto } from './dto/update-admin-settings.dto'; |
| 5 | +import { CreateAdminSettingsDto } from './dto/create-admin-settings.dto'; |
| 6 | + |
| 7 | +@Controller('admin-settings') |
| 8 | +export class AdminSettingsController { |
| 9 | + constructor(private readonly adminSettingsService: AdminSettingsService) {} |
| 10 | + |
| 11 | + @Get() |
| 12 | + async getSettings(): Promise<AdminSettings | null> { |
| 13 | + return this.adminSettingsService.getSettings(); |
| 14 | + } |
| 15 | + |
| 16 | + @Post() |
| 17 | + async createSettings( |
| 18 | + @Body() createAdminSettingsDto: CreateAdminSettingsDto, |
| 19 | + ): Promise<AdminSettings> { |
| 20 | + const settings = { |
| 21 | + ...createAdminSettingsDto, |
| 22 | + socialLinks: createAdminSettingsDto.socialLinks |
| 23 | + ? new Map(Object.entries(createAdminSettingsDto.socialLinks)) |
| 24 | + : new Map(), |
| 25 | + workHours: createAdminSettingsDto.workHours |
| 26 | + ? new Map(Object.entries(createAdminSettingsDto.workHours)) |
| 27 | + : new Map(), |
| 28 | + } as unknown as Omit<AdminSettings, 'id'>; |
| 29 | + |
| 30 | + return this.adminSettingsService.createSettings(settings); |
| 31 | + } |
| 32 | + |
| 33 | + @Put() |
| 34 | + async updateSettings( |
| 35 | + @Body() updateAdminSettingsDto: UpdateAdminSettingsDto, |
| 36 | + ): Promise<AdminSettings> { |
| 37 | + const settings = { |
| 38 | + ...updateAdminSettingsDto, |
| 39 | + socialLinks: updateAdminSettingsDto.socialLinks |
| 40 | + ? new Map(Object.entries(updateAdminSettingsDto.socialLinks)) |
| 41 | + : undefined, |
| 42 | + workHours: updateAdminSettingsDto.workHours |
| 43 | + ? new Map(Object.entries(updateAdminSettingsDto.workHours)) |
| 44 | + : undefined, |
| 45 | + }; |
| 46 | + |
| 47 | + // Remove undefined keys to avoid overriding with undefined |
| 48 | + Object.keys(settings).forEach( |
| 49 | + (key) => settings[key] === undefined && delete settings[key], |
| 50 | + ); |
| 51 | + |
| 52 | + return this.adminSettingsService.updateSettings( |
| 53 | + settings as unknown as Omit<AdminSettings, 'id'>, |
| 54 | + ); |
| 55 | + } |
| 56 | +} |
0 commit comments