public async Task <UpdatedPacketResponse> Handle(Command request, CancellationToken cancellationToken) { var packetToCheck = await PacketRepository.GetPacketWithVersionsByUid(request.PacketUid); var countriesPacketIsAvaliableIn = await PacketRepository.GetCountriesPacketIsAvaliableIn(request.PacketUid); bool packetAvaliableInCountry = countriesPacketIsAvaliableIn.Any(x => x.Code.Equals(request.CountryCode.Value, StringComparison.OrdinalIgnoreCase)); bool packetReleaseDateIsInThePast = DateTime.UtcNow > packetToCheck.Versions.Single(x => x.VersionNumber == request.PacketVersion.VersionCode).VersionPublish; bool packetVersionNotMultiStage = request.PacketVersion.VersionCode - 1 == request.CurrentVersion.VersionCode; var errorResponse = new ErrorResponse(); if (!packetAvaliableInCountry) { errorResponse.AddError(ErrorCodes.PacketNotAvaliableInCountry); } if (!packetReleaseDateIsInThePast) { errorResponse.AddError(ErrorCodes.PacketNotReleased); } if (!packetVersionNotMultiStage) { errorResponse.AddError(ErrorCodes.PacketVersionMultiStage); } errorResponse.ThrowIfErrors(); return(new UpdatedPacketResponse( packetToCheck.Versions.Single(x => x.VersionNumber == request.PacketVersion.VersionCode).VersionCode, packetToCheck.Versions.Single(x => x.VersionNumber == request.CurrentVersion.VersionCode).VersionCode)); }
public async Task <IActionResult> Edit(Guid id, [Bind("EmployeeID,EmployeeNumber,FirstName,LastName,CompanyEmail,PersonalEmail,IsActive,EmployeeID,ReportsTo,CanApprove")] UpdateEmployeeViewModel model) { if (id != model.EmployeeID) { return(NotFound()); } ViewData["Approvers"] = GetApprovers(model.ReportsTo); if (ModelState.IsValid) { try { await Mediator.Send(new UpdateEmployee_Request { FirstName = model.FirstName, LastName = model.LastName, EmployeeNumber = model.EmployeeNumber, CompanyEmail = model.CompanyEmail, PersonalEmail = model.PersonalEmail, ModifiedBy = User.Identity.Name, IsActive = model.IsActive, EmployeeID = model.EmployeeID, ReportsTo = model.ReportsTo, CanApprove = model.CanApprove, }); return(Redirect(Url.Action("Index", "Employees")).WithSuccess("Success", "Updated employee details")); } catch (Exception ex) { ErrorResponse.AddError(ex.GetExceptionMessage()); } } return(View(model).WithDanger("Action cannot be completed", ErrorResponse.Message)); }
public async Task <IActionResult> DeleteConfirmed(Guid id) { var response = await Mediator.Send(new GetEmployeeByIDRequest { EmployeeID = id }); try { var account = await UserManager.FindByEmailAsync(response.CompanyEmail); var result = await UserManager.DeleteAsync(account); if (result.Succeeded) { await Mediator.Send(new DeleteEmployeeRequest { EmployeeID = id }); return(RedirectToAction(nameof(Index)).WithInfo("Prompt", "Successfully removed.")); } }catch (Exception ex) { ErrorResponse.AddError(ex.GetExceptionMessage()); } return(View(new DeleteEmployeeViewModel { FirstName = response.FirstName, LastName = response.LastName, EmployeeNumber = response.EmployeeNumber, CompanyEmail = response.CompanyEmail, PersonalEmail = response.PersonalEmail, ModifiedBy = "N/A", IsActive = response.IsActive }).WithWarning("Action cancelled", ErrorResponse.Message)); }
public async Task <IActionResult> Create([Bind("EmployeeNumber,FirstName,LastName,CompanyEmail,PersonalEmail,ReportsTo,CanApprove")] CreateEmployeeViewModel model) { ViewData["Approvers"] = GetApprovers(model.ReportsTo); if (ModelState.IsValid) { try { await Mediator.Send(new CreateEmployee_Request { FirstName = model.FirstName, LastName = model.LastName, EmployeeNumber = model.EmployeeNumber, CompanyEmail = model.CompanyEmail, PersonalEmail = model.PersonalEmail, CreatedBy = User.Identity.Name, ApproverID = model.ReportsTo, CanApprove = model.CanApprove }); return(Redirect(Url.Action("Index", "Employees")).WithSuccess("Success", "Added new employee record")); }catch (DbUpdateException ex) { ErrorResponse.AddError(ex.GetExceptionMessage()); }catch (Exception ex) { ErrorResponse.AddError(ex.GetExceptionMessage()); } } return(View(model).WithDanger("Action cannot be completed", ErrorResponse.Message)); }
public static PagingValue Create(int?page, int?pageSize, int maxPageSize = maxPageCount) { var errorResponse = new ErrorResponse(); if (page.HasValue && page.Value <= 0) { errorResponse.AddError(ErrorCodes.PagingValuePageMustBePositive); errorResponse.ThrowIfErrors(); } int pageValue; if (page.HasValue) { pageValue = page.Value - 1; } else { pageValue = 0; } int pageSizeValue = pageSize ?? maxPageCount; pageSizeValue = Math.Min(pageSizeValue, maxPageCount); int skip = pageValue * pageSizeValue; int take = pageSizeValue; return(new PagingValue(pageValue, pageSizeValue, skip, take)); }
public static PacketVersionValue CheckIfVersionToUpdateIsNotDowngrade(int currentVersionCode, int versionCode) { var errorResponse = new ErrorResponse(); if (currentVersionCode >= versionCode) { errorResponse.AddError(ErrorCodes.CantDowngradePackageVersion); } errorResponse.ThrowIfErrors(); return(new PacketVersionValue(versionCode)); }
public static PacketVersionValue Create(int versionCode) { var errorResponse = new ErrorResponse(); if (versionCode < 0) { errorResponse.AddError(ErrorCodes.VersionCodeInvalid); } errorResponse.ThrowIfErrors(); return(new PacketVersionValue(versionCode)); }
public ErrorResponse CreateErrorResponse() { ErrorResponse returnValue = new ErrorResponse(); Exception ex = _exceptionHandler.Error; if (ex as AggregateException != null) { AggregateException aggregateException = ex as AggregateException; returnValue.AddErrors(ProcessAggregateException(aggregateException)); } if (ex as ApiCallFailedException != null) { ApiCallFailedException apiCallFailedException = ex as ApiCallFailedException; returnValue.AddError(ProcessApiCallFailedException(apiCallFailedException)); } returnValue.AddError(_httpContext.TraceIdentifier, 500, ex.Message); return(returnValue); }
public static CountryCodeValue Create(string value) { value = value.ToLower().Trim(); var errorResponse = new ErrorResponse(); if (value.Length < 1) { errorResponse.AddError(ErrorCodes.CountryCodeValidationEmpty); } if (value.Length != CountryCodeLength) { errorResponse.AddError(ErrorCodes.CountryCodeValidationLength); } if (new CountryHelper().GetCountry(value) == null) { errorResponse.AddError(ErrorCodes.CountryCodeInvalidCountry); } errorResponse.ThrowIfErrors(); return(new CountryCodeValue(value)); }
private Task HandleExceptionAsync(HttpContext context, Exception exception) { LogException(exception); ErrorResponse errorResponse = new ErrorResponse { StatusCode = context.Response.StatusCode }; if (exception is DomainException domainException) { context.Response.StatusCode = (int)HttpStatusCode.BadRequest; foreach (var error in domainException.DomainErrors) { errorResponse.AddError( code: error.ErrorCode, userMessage: error.ErrorMessage, details: $"Validation for '{error.PropertyName}' with value '{error.AttemptedValue}' failed in {error.ClassName}", message: "Validation failed"); } } else { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; errorResponse.AddError( code: nameof(HttpStatusCode.InternalServerError), details: exception.StackTrace, message: exception.Message, userMessage: string.Empty); } context.Response.ContentType = "application/json"; return(context.Response.WriteAsync(errorResponse.ToString())); }
public override void OnActionExecuting(ActionExecutingContext context) { Patient patient = null; var error = new ErrorResponse(); foreach (var argument in context.ActionArguments.Values.Where(v => v is Patient)) { patient = argument as Patient; break; } if (patient == null) { error.AddError(nameof(patient), "Body is not a json representing patient data"); } else { if (string.IsNullOrEmpty(patient.Name)) { error.AddError(nameof(patient.Name), "Field is mandatory"); } if (string.IsNullOrEmpty(patient.FileNo)) { error.AddError(nameof(patient.FileNo), "Field is mandatory"); } else { if (!int.TryParse(patient.FileNo, out int fileNumber)) { error.AddError(nameof(patient.FileNo), "Field is not in correct format"); } } if (string.IsNullOrEmpty(patient.Gender)) { error.AddError(nameof(patient.Gender), "Field is mandatory"); } else { if (!Enum.TryParse(patient.Gender, out Gender sex)) { error.AddError(nameof(patient.Gender), "Field is not in correct format"); } } if (!string.IsNullOrEmpty(patient.Vip)) { if (!bool.TryParse(patient.Vip, out bool isVip)) { error.AddError(nameof(patient.Vip), "Field is not in correct format"); } } if (string.IsNullOrEmpty(patient.PhoneNumber)) { error.AddError(nameof(patient.PhoneNumber), "Field is mandatory"); } else { if (!IsValidPhoneNumber(patient.PhoneNumber)) { error.AddError(nameof(patient.PhoneNumber), "Field is not in correct format"); } } if (!string.IsNullOrEmpty(patient.Email) && !IsValidEmail(patient.Email)) { error.AddError(nameof(patient.Email), "Field is not in correct format"); } if (!string.IsNullOrEmpty(patient.Birthdate)) { if (!IsValidDateTime(patient.Birthdate)) { error.AddError(nameof(patient.Birthdate), "Field is not in correct format"); } } if (!string.IsNullOrEmpty(patient.FirstVisitDate)) { if (!IsValidDateTime(patient.FirstVisitDate)) { error.AddError(nameof(patient.FirstVisitDate), "Field is not in correct format"); } } } if (error.Errors.Any()) { context.Result = new BadRequestObjectResult(error); return; } }