Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CONTROLLER:Post Video metadata #12

Merged
merged 9 commits into from
Jun 12, 2024
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
52 changes: 52 additions & 0 deletions UsefulTime.Api/Controllers/VideoMetadataController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//=================================================
//Copyright (c) Coalition of Good-Hearted Engineers
//Free To Use To Find Comfort and Pease
//=================================================
using Microsoft.AspNetCore.Mvc;
using RESTFulSense.Controllers;
using UsefulTime.Api.Models.VideoMetadatas.Exceptions;
using UsefulTime.Api.Models.VideoMetadatas;
using UsefulTime.Api.Services.Foundations.VideoMetadatas;

namespace UsefulTime.Api.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class VideoMetadataController:RESTFulController
{
private readonly IVideoMetadataService videoMetadataService;
public VideoMetadataController(IVideoMetadataService videoMetadataService)
{
this.videoMetadataService = videoMetadataService;
}
[HttpPost]
public async ValueTask<ActionResult<VideoMetadata>> PostVideoMetadataAsync(VideoMetadata videoMetadata)
{
try
{
VideoMetadata postedVideoMetadata =
await this.videoMetadataService.AddVideoMetadataAsync(videoMetadata);

return Created(postedVideoMetadata);
}
catch (VideoMetadataValidationException videoMetadataValidationException)
{
return BadRequest(videoMetadataValidationException.InnerException);
}
catch (VideoMetadataDependencyValidationException videoMetadataDependencyValidationException)
when (videoMetadataDependencyValidationException.InnerException is
AlreadyExistVideoMetadataException)
{
return Conflict(videoMetadataDependencyValidationException.InnerException);
}
catch (VideoMetadataDependencyException videoMetadataDependencyException)
{
return InternalServerError(videoMetadataDependencyException);
}
catch (VideoMetadataServiceException videoMetadataServiceException)
{
return InternalServerError(videoMetadataServiceException);
}
}
}
}
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
Loading