Compare commits
7 commits
e3a1888146
...
87a2250852
Author | SHA1 | Date | |
---|---|---|---|
87a2250852 | |||
e7b737a3dd | |||
69573991ca | |||
126e2a8c73 | |||
![]() |
84e16d655d | ||
![]() |
1b54a6d6a6 | ||
![]() |
4216535016 |
6 changed files with 62 additions and 14 deletions
|
@ -4,4 +4,5 @@ public class ProductTableGet
|
||||||
{
|
{
|
||||||
public string ProductTableName { get; set; }
|
public string ProductTableName { get; set; }
|
||||||
public string ProductTableId { get; set; }
|
public string ProductTableId { get; set; }
|
||||||
|
public string CompanyTableId { get; set; }
|
||||||
}
|
}
|
|
@ -35,7 +35,7 @@ public class AdminOnlyHandler : AuthorizationHandler<AdminOnlyRequirement>
|
||||||
}
|
}
|
||||||
catch (NotFoundException _)
|
catch (NotFoundException _)
|
||||||
{
|
{
|
||||||
throw new ForbiddenException();
|
throw new ForbiddenException("You need to be logged in to do this action.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_userProfileService.IsUserProfileAdmin(userProfile))
|
if (_userProfileService.IsUserProfileAdmin(userProfile))
|
||||||
|
|
|
@ -1,38 +1,70 @@
|
||||||
namespace DrinkRateAPI.Exceptions;
|
namespace DrinkRateAPI.Exceptions;
|
||||||
|
|
||||||
public abstract class DrinkRateException : Exception;
|
public class DrinkRateException : Exception
|
||||||
|
{
|
||||||
|
public DrinkRateException() : base() { }
|
||||||
|
public DrinkRateException(string message) : base(message) { }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 400 - Bad request
|
/// 400 - Bad request
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class BadRequestException : DrinkRateException;
|
public class BadRequestException : DrinkRateException
|
||||||
|
{
|
||||||
|
public BadRequestException() : base() { }
|
||||||
|
public BadRequestException(string message) : base(message) { }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 401 - Unauthenticated
|
/// 401 - Unauthenticated
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class UnauthenticatedException : DrinkRateException;
|
public class UnauthenticatedException : DrinkRateException
|
||||||
|
{
|
||||||
|
public UnauthenticatedException() : base() { }
|
||||||
|
public UnauthenticatedException(string message) : base(message) { }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 402 - Payment required
|
/// 402 - Payment required
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class PaymentRequiredException : DrinkRateException;
|
public class PaymentRequiredException : DrinkRateException
|
||||||
|
{
|
||||||
|
public PaymentRequiredException() : base() { }
|
||||||
|
public PaymentRequiredException(string message) : base(message) { }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 403 - Forbidden
|
/// 403 - Forbidden
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ForbiddenException : DrinkRateException;
|
public class ForbiddenException : DrinkRateException
|
||||||
|
{
|
||||||
|
public ForbiddenException() : base() { }
|
||||||
|
public ForbiddenException(string message) : base(message) { }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 404 - Not found
|
/// 404 - Not found
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class NotFoundException : DrinkRateException;
|
public class NotFoundException : DrinkRateException
|
||||||
|
{
|
||||||
|
public NotFoundException() : base() { }
|
||||||
|
public NotFoundException(string message) : base(message) { }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 418 - I'm a teapot
|
/// 418 - I'm a teapot
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class IamATeapotException : DrinkRateException;
|
public class IamATeapotException : DrinkRateException
|
||||||
|
{
|
||||||
|
public IamATeapotException() : base() { }
|
||||||
|
public IamATeapotException(string message) : base(message) { }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 451 - Unavailable for lagal reasons
|
/// 451 - Unavailable for lagal reasons
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class UnavailableForLagalReasonsException : DrinkRateException;
|
public class UnavailableForLagalReasonsException : DrinkRateException
|
||||||
|
{
|
||||||
|
public UnavailableForLagalReasonsException() : base() { }
|
||||||
|
public UnavailableForLagalReasonsException(string message) : base(message) { }
|
||||||
|
}
|
|
@ -13,7 +13,7 @@ public class ApplicationUserService(ApplicationDbContext context)
|
||||||
{
|
{
|
||||||
var appUserId = identity.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
var appUserId = identity.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||||
var profile = await _context.UserProfiles
|
var profile = await _context.UserProfiles
|
||||||
.FirstOrDefaultAsync(x => x.ApplicationUserId.ToString() == appUserId)
|
.FirstOrDefaultAsync(x => x.ApplicationUserId.ToString() == appUserId && !x.IsDeleted)
|
||||||
?? throw new NotFoundException();
|
?? throw new NotFoundException();
|
||||||
|
|
||||||
return profile;
|
return profile;
|
||||||
|
|
|
@ -4,6 +4,7 @@ using DrinkRateAPI.Contexts;
|
||||||
using DrinkRateAPI.DbEntities;
|
using DrinkRateAPI.DbEntities;
|
||||||
using DrinkRateAPI.Exceptions;
|
using DrinkRateAPI.Exceptions;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace DrinkRateAPI.Services;
|
namespace DrinkRateAPI.Services;
|
||||||
|
|
||||||
|
@ -24,7 +25,20 @@ public class ProductCompanyTableCoupleService(ApplicationDbContext context)
|
||||||
};
|
};
|
||||||
|
|
||||||
_context.CompanyTable.Add(companyTable);
|
_context.CompanyTable.Add(companyTable);
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
catch (DbUpdateException ex) when
|
||||||
|
(ex.InnerException is PostgresException
|
||||||
|
{
|
||||||
|
SqlState: PostgresErrorCodes.UniqueViolation
|
||||||
|
}
|
||||||
|
)
|
||||||
|
{
|
||||||
|
throw new BadRequestException("Product table or company table with the same name already exists.");
|
||||||
|
}
|
||||||
|
|
||||||
return await GetProductCompanyTableCouplePostResponse(
|
return await GetProductCompanyTableCouplePostResponse(
|
||||||
productCompanyTableCouplePost.ProductTableName,
|
productCompanyTableCouplePost.ProductTableName,
|
||||||
|
|
|
@ -15,12 +15,13 @@ public class ProductTableService(ApplicationDbContext context)
|
||||||
{
|
{
|
||||||
var productTable =
|
var productTable =
|
||||||
await _context.ProductTable.FirstOrDefaultAsync(x => x.ProductTableName == productTableName) ??
|
await _context.ProductTable.FirstOrDefaultAsync(x => x.ProductTableName == productTableName) ??
|
||||||
throw new NotFoundException();
|
throw new NotFoundException($"Product table with the name {productTableName} not found.");
|
||||||
|
|
||||||
return new ProductTableGet
|
return new ProductTableGet
|
||||||
{
|
{
|
||||||
ProductTableName = productTable.ProductTableName,
|
ProductTableName = productTable.ProductTableName,
|
||||||
ProductTableId = productTable.Id.ToString()
|
ProductTableId = productTable.Id.ToString(),
|
||||||
|
CompanyTableId = productTable.CompanyTableId.ToString(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue