From 33c49e8a48b999414ffdd119152eaa054764f697 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Thu, 28 Sep 2023 17:50:58 +0500 Subject: [PATCH] replace ArgumentInvalidException by ValidationBadRequest in Controllers --- AsbCloudWebApi/Controllers/DailyReportController.cs | 5 +++-- AsbCloudWebApi/Controllers/DrillingProgramController.cs | 4 ++-- AsbCloudWebApi/Controllers/NotificationController.cs | 5 +++-- AsbCloudWebApi/Controllers/UserSettingsController.cs | 6 ++++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/AsbCloudWebApi/Controllers/DailyReportController.cs b/AsbCloudWebApi/Controllers/DailyReportController.cs index 45d7f135..dc740619 100644 --- a/AsbCloudWebApi/Controllers/DailyReportController.cs +++ b/AsbCloudWebApi/Controllers/DailyReportController.cs @@ -169,8 +169,9 @@ namespace AsbCloudWebApi.Controllers if (!await UserHasAccesToWellAsync(idWell, token)) return Forbid(); - var well = await wellService.GetOrDefaultAsync(idWell, token) - ?? throw new ArgumentInvalidException($"Скважина c id:{idWell} не найдена", nameof(idWell)); + var well = await wellService.GetOrDefaultAsync(idWell, token); + if (well is null) + return this.ValidationBadRequest(nameof(idWell), $"Скважина c id:{idWell} не найдена"); var stream = await dailyReportService.MakeReportAsync(idWell, date, token); if (stream is null) diff --git a/AsbCloudWebApi/Controllers/DrillingProgramController.cs b/AsbCloudWebApi/Controllers/DrillingProgramController.cs index 435f0b50..0d22fa34 100644 --- a/AsbCloudWebApi/Controllers/DrillingProgramController.cs +++ b/AsbCloudWebApi/Controllers/DrillingProgramController.cs @@ -119,10 +119,10 @@ namespace AsbCloudWebApi.Controllers return Forbid(); if (files.Count > 1) - throw new ArgumentInvalidException("only 1 file can be uploaded", nameof(files)); + return this.ValidationBadRequest(nameof(files), "only 1 file can be uploaded"); if (files.Count == 0) - throw new ArgumentInvalidException("at list 1 file should be uploaded", nameof(files)); + return this.ValidationBadRequest(nameof(files), "at list 1 file should be uploaded"); var fileName = files[0].FileName; diff --git a/AsbCloudWebApi/Controllers/NotificationController.cs b/AsbCloudWebApi/Controllers/NotificationController.cs index 3332e012..b8a8aaea 100644 --- a/AsbCloudWebApi/Controllers/NotificationController.cs +++ b/AsbCloudWebApi/Controllers/NotificationController.cs @@ -87,8 +87,9 @@ public class NotificationController : ControllerBase public async Task GetAsync([Required] int idNotification, CancellationToken cancellationToken) { - var notification = await notificationRepository.GetOrDefaultAsync(idNotification, cancellationToken) - ?? throw new ArgumentInvalidException("Уведомление не найдено", nameof(idNotification)); + var notification = await notificationRepository.GetOrDefaultAsync(idNotification, cancellationToken); + if (notification is null) + return this.ValidationBadRequest(nameof(idNotification), "Уведомление не найдено"); return Ok(notification); } diff --git a/AsbCloudWebApi/Controllers/UserSettingsController.cs b/AsbCloudWebApi/Controllers/UserSettingsController.cs index db3f6f2e..f0325b44 100644 --- a/AsbCloudWebApi/Controllers/UserSettingsController.cs +++ b/AsbCloudWebApi/Controllers/UserSettingsController.cs @@ -64,7 +64,8 @@ namespace AsbCloudWebApi.Controllers var result = await service.UpsertAsync((int)userId, key, value, token).ConfigureAwait(false); if (result < 0) - throw new ArgumentInvalidException("not found", nameof(key)); + return this.ValidationBadRequest(nameof(key), "not found"); + return Ok(result); } @@ -84,7 +85,8 @@ namespace AsbCloudWebApi.Controllers var result = await service.DeleteAsync((int)userId, key, token).ConfigureAwait(false); if (result < 0) - throw new ArgumentInvalidException("not found", nameof(key)); + return this.ValidationBadRequest(nameof(key), "not found"); + return Ok(result); }