Skip to content

Commit

Permalink
Delete approvement dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Dębowski committed Dec 3, 2024
1 parent 1aea309 commit 9a9917e
Show file tree
Hide file tree
Showing 27 changed files with 256 additions and 20 deletions.
11 changes: 11 additions & 0 deletions src/hiPower.Common.Type/Errors/DataCenterErrors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using ErrorOr;

namespace hiPower.Common.Type.Errors;

public static class DataCenterErrors
{
public static Error DeleteError => Error.Failure (
code : "DeleteError",
description: "Unable to delete the data center."
);
}
7 changes: 7 additions & 0 deletions src/hiPower.Core/DataCenterService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using hiPower.Abstracts;
using hiPower.Common.Type.Errors;
using MapsterMapper;

namespace hiPower.Core;
Expand All @@ -15,7 +16,13 @@ public async Task<ErrorOr<DataCenter>> CreateAsync (DataCenter location)

public async Task<ErrorOr<bool>> DeleteAsync (string id)
{
var servers = await unit.ServerRepository.GetAll(x => x.LocationId.Equals(id.ToUpper()), null, null);
if (servers.Any())
{
return DataCenterErrors.DeleteError;
}
await unit.DataCenterRepository.DeleteAsync(id);
await unit.SaveAsync ();
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/hiPower.WebApi/Controllers/DataCentersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace hiPower.WebApi.Controllers
public class DataCentersController(IDataCenterService locationService) : ControllerBase
{
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK, Type= typeof (ApiResult<IEnumerable<DataCenter>>))]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof (ApiResult<IEnumerable<DataCenter>>))]
public async Task<IActionResult> GetAll()
{
var result = await locationService.GetAsync();
Expand All @@ -26,7 +26,7 @@ public async Task<IActionResult> GetAll()


[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK, Type=typeof(ApiResult<DataCenter>))]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ApiResult<DataCenter>))]
[ProducesResponseType (StatusCodes.Status400BadRequest, Type = typeof (ProblemDetails))]
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ProblemDetails))]
public async Task<IActionResult> Get ([FromRoute] string id)
Expand Down Expand Up @@ -99,7 +99,7 @@ public async Task<IActionResult> Update ([FromRoute] string id, [FromBody] DataC
return Ok (response);
}

[HttpDelete ("id")]
[HttpDelete ("{id}")]
[ProducesResponseType (StatusCodes.Status200OK, Type = typeof (ApiResult<bool>))]
[ProducesResponseType (StatusCodes.Status400BadRequest, Type = typeof (ProblemDetails))]
[ProducesResponseType (StatusCodes.Status404NotFound, Type = typeof (ProblemDetails))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static IServiceCollection AddCorsConfiguration (this IServiceCollection s
setup.AddPolicy (CorsPolicyName, builder => {
builder.AllowAnyOrigin ();
builder.AllowAnyHeader ();
builder.AllowAnyMethod ();
builder.WithMethods("GET", "POST", "PUT", "DELETE", "OPTIONS");
});
});
return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export class CreateNewComponent {
city: formData.city,
postalCode: formData.postalCode,
region: formData.region,
country: formData.country
country: formData.country,
description: formData.description
});

save$.subscribe({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ConfigService } from 'src/app/initializer/config.service';

import { CreateNewComponent } from './create-new/create-new.component';
import { EditDataCenterComponent } from './edit-data-center/edit-data-center.component';
import { MAT_DIALOG_DEFAULT_OPTIONS } from '@angular/material/dialog';

@NgModule({
declarations: [
Expand All @@ -31,7 +32,11 @@ import { EditDataCenterComponent } from './edit-data-center/edit-data-center.com
providers: [
DataCenterService,
ConfigService,
LoadingService
LoadingService,
{
provide: MAT_DIALOG_DEFAULT_OPTIONS,
useValue: { hasBackdrop: false }
}
]
})
export class DataCenterModule { }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p>Data centers</p>
<loading></loading>
<loading [detectRoutingOngoing]="true"></loading>
<mat-card>
<mat-card-header>
<button mat-raised-button routerLink="create"><mat-icon>add</mat-icon>Create a new data center</button>
Expand Down Expand Up @@ -34,7 +34,7 @@
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let item">
<a mat-icon-button aria-label="edit item" [routerLink]="[item.id]" ><mat-icon>edit</mat-icon></a>
<button mat-icon-button aria-label="delete item"><mat-icon>delete</mat-icon></button>
<button mat-icon-button aria-label="delete item" (click)="openDialog('10ms', '10ms', item.id)"><mat-icon>delete</mat-icon></button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columns"></tr>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { Component, OnInit } from '@angular/core';
import { ChangeDetectionStrategy, Component,
OnInit,
inject } from '@angular/core';
import { DataCenterService } from '../services/data-center.service';
import { EMPTY, Observable, tap } from 'rxjs';
import { EMPTY,
filter,
Observable} from 'rxjs';
import { DataCenter } from '../core/models/data-center';
import { LoadingService } from '@shared/components/loading/loading.service';
import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
import { DeleteDialogComponent } from '@shared/components/dialogs/delete/delete.component';

@Component({
selector: 'app-home-datacenters',
templateUrl: './home.component.html',
styleUrl: './home.component.scss'
styleUrl: './home.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class HomeComponent implements OnInit {
readonly dialog = inject(MatDialog);

datacenters$: Observable<DataCenter[]> = EMPTY;

columns = [
'name',
'city',
Expand All @@ -29,12 +37,46 @@ export class HomeComponent implements OnInit {
}

ngOnInit(): void {
const loading$ = this.dataCenterService.getDataCentres();
this.datacenters$ = this.loadinService
.showLoaderUntilCompleted<DataCenter[]>(loading$);
this.loadData();
}

openDialog(enterAnimationDuration: string, exitAnimtionDuration: string, dataCenterId: string): void {
const dialogData = {
title: 'Delete data center',
message: 'Are you sure?',
}

const dialogConfig: MatDialogConfig = {
width: '250px',
enterAnimationDuration: enterAnimationDuration,
exitAnimationDuration: exitAnimtionDuration,
data: dialogData
};

this.dialog
.open(DeleteDialogComponent, dialogConfig)
.afterClosed()
.pipe(
filter(x => x == true)
)
.subscribe({
next: () => this.deleteDataCenter(dataCenterId)
});
}

onEdit(item: DataCenter): void {
deleteDataCenter(dataCenterId: string): void {
const delete$ = this.dataCenterService
.deleteDataCenter(dataCenterId);
this.loadinService
.showLoaderUntilCompleted(delete$)
.subscribe({
complete: () => this.loadData()
});
}

private loadData(): void {
const loading$ = this.dataCenterService.getDataCentres();
this.datacenters$ = this.loadinService
.showLoaderUntilCompleted<DataCenter[]>(loading$);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ export class DataCenterService extends DataService {
map(x => x as DataCenter)
);
}

deleteDataCenter(id: string): Observable<boolean> {
return this.delete(`${this.apiUrl}/${id}`)
.pipe(
map((x: ApiResponse<boolean>) => x.success)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>common-module works!</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { CommonModuleComponent } from './common-module.component';

describe('CommonModuleComponent', () => {
let component: CommonModuleComponent;
let fixture: ComponentFixture<CommonModuleComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [CommonModuleComponent]
})
.compileComponents();

fixture = TestBed.createComponent(CommonModuleComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Component } from '@angular/core';

@Component({
selector: 'common-module',
templateUrl: './common-module.component.html',
styleUrl: './common-module.component.scss'
})
export class CommonModuleComponent {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>log-msgs-module works!</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { LogMsgsModuleComponent } from './log-msgs-module.component';

describe('LogMsgsModuleComponent', () => {
let component: LogMsgsModuleComponent;
let fixture: ComponentFixture<LogMsgsModuleComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [LogMsgsModuleComponent]
})
.compileComponents();

fixture = TestBed.createComponent(LogMsgsModuleComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Component } from '@angular/core';

@Component({
selector: 'log-msgs-module',
templateUrl: './log-msgs-module.component.html',
styleUrl: './log-msgs-module.component.scss'
})
export class LogMsgsModuleComponent {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>remote-module works!</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { RemoteModuleComponent } from './remote-module.component';

describe('RemoteModuleComponent', () => {
let component: RemoteModuleComponent;
let fixture: ComponentFixture<RemoteModuleComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [RemoteModuleComponent]
})
.compileComponents();

fixture = TestBed.createComponent(RemoteModuleComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Component } from '@angular/core';

@Component({
selector: 'remote-module',
templateUrl: './remote-module.component.html',
styleUrl: './remote-module.component.scss'
})
export class RemoteModuleComponent {

}
6 changes: 3 additions & 3 deletions src/powerdns-manager/src/app/shared/base/data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ export class DataService {

private handleError(error: Response): Observable<never> {
if (error.status === NotFoundCode) {
return throwError(() => new NotFoundError());
return throwError(() => new NotFoundError(error));
}

if (error.status === BadInputCode ) {
return throwError(() => new BadInputError());
return throwError(() => new BadInputError(error));
}

return throwError(() => new ApplicationError());
return throwError(() => new ApplicationError(error));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2 mat-dialog-title>{{ data.title }}</h2>
<mat-dialog-content>
{{ data.message }}
</mat-dialog-content>
<mat-dialog-actions>
<button type="button"
aria-label="Disagee action"
mat-button
mat-dialog-close>No</button>
<button type="button"
aria-label="Approve action"
mat-button
mat-dialog-close
cdkFocusInit
[mat-dialog-close]="true">Delete</button>
</mat-dialog-actions>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { DeleteComponent } from './delete.component';

describe('DeleteComponent', () => {
let component: DeleteComponent;
let fixture: ComponentFixture<DeleteComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [DeleteComponent]
})
.compileComponents();

fixture = TestBed.createComponent(DeleteComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Loading

0 comments on commit 9a9917e

Please sign in to comment.