Skip to content

Commit

Permalink
Merge pull request #11 from JaloliddinDeveloper/users/JaloliddinDevel…
Browse files Browse the repository at this point in the history
…oper/foundations-add-test

FOUNDATIONS:Tests
  • Loading branch information
JaloliddinDeveloper authored Jun 12, 2024
2 parents 9c42209 + efa2c6c commit 7ecd18d
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 54 deletions.
2 changes: 1 addition & 1 deletion UsefulTime.Api/Brokers/DateTimes/IDateTimeBroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//Free To Use To Find Comfort and Pease
//=================================================
namespace UsefulTime.Api.Brokers.DateTimes
{
{
public interface IDateTimeBroker
{
DateTimeOffset GetCurrentDateTimeOffset();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//=================================================
//Copyright (c) Coalition of Good-Hearted Engineers
//Free To Use To Find Comfort and Pease
//=================================================
using Xeptions;

namespace UsefulTime.Api.Models.VideoMetadatas.Exceptions
{
public class LockedVideoMetadataException:Xeption
{
public LockedVideoMetadataException(string message, Exception innerException)
: base(message, innerException)
{ }
}
}
2 changes: 2 additions & 0 deletions UsefulTime.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//Copyright (c) Coalition of Good-Hearted Engineers
//Free To Use To Find Comfort and Pease
//=================================================
using UsefulTime.Api.Brokers.DateTimes;
using UsefulTime.Api.Brokers.Loggings;
using UsefulTime.Api.Brokers.Storages;
using UsefulTime.Api.Services.Foundations.VideoMetadatas;
Expand All @@ -18,6 +19,7 @@ private static void Main(string[] args)
builder.Services.AddSwaggerGen();
builder.Services.AddTransient<IStorageBroker, StorageBroker>();
builder.Services.AddTransient<ILoggingBroker, LoggingBroker>();
builder.Services.AddTransient<IDateTimeBroker,DateTimeBroker>();
builder.Services.AddTransient<IVideoMetadataService, VideoMetadataService>();

var app = builder.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//=================================================
using EFxceptions.Models.Exceptions;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using UsefulTime.Api.Models.VideoMetadatas;
using UsefulTime.Api.Models.VideoMetadatas.Exceptions;
using Xeptions;
Expand Down Expand Up @@ -43,10 +44,28 @@ private async ValueTask<VideoMetadata> TryCatch(ReturningVideoMetadataFunction r
innerException: duplicateKeyException);
throw CreateAndDependencyValidationException(alreadyExistVideoMetadataException);
}
catch (DbUpdateConcurrencyException dbUpdateConcurrencyException)
{
LockedVideoMetadataException lockedVideoMetadataException = new LockedVideoMetadataException(
"Video Metadata is locked, please try again",
dbUpdateConcurrencyException);

throw CreateAndLogDependencyValidationException(lockedVideoMetadataException);
}
catch (DbUpdateException databaseUpdateException)
{
var failedVideoMetadataStorageException = new FailedVideoMetadataStorageException(
message: "Failed video metadata error occured, contact support",
innerException: databaseUpdateException);

throw CreateAndLogDependencyException(failedVideoMetadataStorageException);
}
catch (Exception exception)
{
var failedVideoMetadataServiceException =
new FailedVideoMetadataServiceException(message: "Failed guest service error occurred,contact support",innerException:exception);
new FailedVideoMetadataServiceException(
message: "Failed guest service error occurred,contact support",
innerException:exception);

throw CreateAndLogServiseException(failedVideoMetadataServiceException);
}
Expand Down Expand Up @@ -81,10 +100,30 @@ public VideoMetadataDependencyValidationException CreateAndDependencyValidationE
{
var videoMetadataDependencyValidationException =
new VideoMetadataDependencyValidationException(
message: "Video metadata dependency error occurred,fix the errors try again",
message: "Video metadata Dependency validation error occured,fix the errors and try again",
innerException:exception );
this.loggingBroker.LogError(videoMetadataDependencyValidationException);
return videoMetadataDependencyValidationException;
}
private Exception CreateAndLogDependencyException(Xeption exception)
{
var videoMetadataDependencyException = new VideoMetadataDependencyException(
message: "Video metadata dependency error occured, fix the errors and try again",
innerException: exception);

this.loggingBroker.LogError(videoMetadataDependencyException);

return videoMetadataDependencyException;
}
private VideoMetadataDependencyValidationException CreateAndLogDependencyValidationException(Xeption exception)
{
var videoMetadataDependencyValidationException = new VideoMetadataDependencyValidationException(
"Video metadata Dependency validation error occured,fix the errors and try again",
exception);

this.loggingBroker.LogError(videoMetadataDependencyValidationException);

return videoMetadataDependencyValidationException;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ private void ValidatevideoMetadataOnAdd(VideoMetadata videoMetadata)
(Rule: IsInvalid(videoMetadata.Title), Parameter: nameof(videoMetadata.Title)),
(Rule: IsInvalid(videoMetadata.BlobPath), Parameter: nameof(videoMetadata.BlobPath)),
(Rule: IsInvalid(videoMetadata.CreatedDate), Parameter: nameof(videoMetadata.CreatedDate)),
(Rule: IsInvalid(videoMetadata.UpdatedDate), Parameter: nameof(videoMetadata.UpdatedDate)));
(Rule: IsInvalid(videoMetadata.UpdatedDate), Parameter: nameof(videoMetadata.UpdatedDate)),
(Rule: IsNotRecent(videoMetadata.CreatedDate), Parameter: nameof(VideoMetadata.CreatedDate)),
(Rule: IsNotSame(
firstDate: videoMetadata.CreatedDate,
secondDate: videoMetadata.UpdatedDate,
secondDateName: nameof(VideoMetadata.UpdatedDate)),

Parameter: nameof(VideoMetadata.CreatedDate)));
}
private void ValidationVideoMetadataNotNull(VideoMetadata videoMetadata)
{
Expand All @@ -42,6 +49,28 @@ private void ValidationVideoMetadataNotNull(VideoMetadata videoMetadata)
Condition = date == default,
Message = "Data is required"
};
private static dynamic IsNotSame(
DateTimeOffset firstDate,
DateTimeOffset secondDate,
string secondDateName) => new
{
Condition = firstDate != secondDate,
Message = $"Date is not same as {secondDateName}"
};

private dynamic IsNotRecent(DateTimeOffset date) => new
{
Condition = IsDateNotRecent(date),
Message = "Date is not recent"
};

private bool IsDateNotRecent(DateTimeOffset date)
{
DateTimeOffset currentDateTime = this.dateTimeBroker.GetCurrentDateTimeOffset();
TimeSpan timeDifference = currentDateTime.Subtract(date);

return timeDifference.TotalSeconds is > 60 or < 0;
}
private static void Validate(params (dynamic Rule, string Parameter)[] validations)
{
var invalidVideoMetadataException =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//Copyright (c) Coalition of Good-Hearted Engineers
//Free To Use To Find Comfort and Pease
//=================================================
using UsefulTime.Api.Brokers.DateTimes;
using UsefulTime.Api.Brokers.Loggings;
using UsefulTime.Api.Brokers.Storages;
using UsefulTime.Api.Models.VideoMetadatas;
Expand All @@ -12,12 +13,18 @@ public partial class VideoMetadataService : IVideoMetadataService
{
private readonly IStorageBroker storageBroker;
private readonly ILoggingBroker loggingBroker;
private readonly IDateTimeBroker dateTimeBroker;

public VideoMetadataService(IStorageBroker storageBroker, ILoggingBroker loggingBroker)
public VideoMetadataService(
IStorageBroker storageBroker,
ILoggingBroker loggingBroker,
IDateTimeBroker dateTimeBroker)
{
this.storageBroker = storageBroker;
this.loggingBroker = loggingBroker;
this.dateTimeBroker = dateTimeBroker;
}

public ValueTask<VideoMetadata> AddVideoMetadataAsync(VideoMetadata videoMetadata) =>
TryCatch(async () =>
{
Expand Down
Loading

0 comments on commit 7ecd18d

Please sign in to comment.