public async Task <IActionResult> Create([FromBody] AddNewRegistrationDto newRegistrationDto) { ErrorMessage ObjectNotAvailable = new ErrorMessage { ErrorCode = "TRANSACTION.OBJECT.RESERVE.NOT.AVAILABLE", Message = "This object is not available", StatusCode = System.Net.HttpStatusCode.BadRequest }; var user = await _userDataManager.AddCurrentUserIfNeeded(); if (user.Login == null) { return(StatusCode(new ErrorMessage() { ErrorCode = "TRANSACTION.OBJECT.RESERVE.NOT.AUTHORIZED", Message = "You are not authorized to do this operation", StatusCode = System.Net.HttpStatusCode.Unauthorized })); } if (newRegistrationDto is null) { return(StatusCode(new ErrorMessage() { ErrorCode = "TRANSACTION.OBJECT.RESERVE.NULL", Message = "Please send a valid information", StatusCode = System.Net.HttpStatusCode.BadRequest })); } var @object = await _objectDataManager.GetObjectAsync(newRegistrationDto.ObjectId); if (@object is null) { return(StatusCode(new ErrorMessage() { ErrorCode = "TRANSACTION.OBJECT.RESERVE.NOT.EXISTS", Message = "The object specified does not exists", StatusCode = System.Net.HttpStatusCode.BadRequest })); } // Should Not Return and is not taken right now if ([email protected]) { var receivings = from receiving in _objectReceiving.Table where receiving.ObjectRegistration.ObjectId == @object.OfferedObjectId select receiving; // If The object has receiving and all of them has returnings if (receivings.Any(r => r.ObjectReturning == null)) { return(StatusCode(ObjectNotAvailable)); } } // See Previous registrations var existingRegistrations = from registration in _registrationsRepo.Table where registration.RecipientLogin.UserId == user.User.UserId && registration.ObjectId == @object.OfferedObjectId select registration; // If The user taken and has this object OR If the user has another registeration pending receiving if (existingRegistrations.Any(reg => reg.ObjectReceiving == null || reg.ObjectReceiving.ObjectReturning == null)) { return(StatusCode(ObjectNotAvailable)); } TimeSpan?shouldReturnItAfter; if (@object.ShouldReturn) { // If the object should return but the user has not specified the time he should return the object if (!newRegistrationDto.ShouldReturnAfter.HasValue) { return(StatusCode(new ErrorMessage { ErrorCode = "TRANSACTION.OBJECT.RESERVE.SHOULDRETURN.NULL", Message = "Please specify when you will return this object", StatusCode = System.Net.HttpStatusCode.BadRequest })); } if (@object.HourlyCharge.HasValue) { shouldReturnItAfter = new TimeSpan(newRegistrationDto.ShouldReturnAfter.Value, 0, 0); } else { if (newRegistrationDto.ShouldReturnAfter > maximumHoursForFreeLending) { shouldReturnItAfter = new TimeSpan(maximumHoursForFreeLending, 0, 0); } else { shouldReturnItAfter = new TimeSpan(newRegistrationDto.ShouldReturnAfter.Value, 0, 0); } } } else { shouldReturnItAfter = null; } var registrationModel = new ObjectRegistration { ObjectRegistrationId = Guid.NewGuid(), RegisteredAtUtc = DateTime.UtcNow, ExpiresAtUtc = DateTime.UtcNow.AddHours(maximumHoursForReservationExpiration), ObjectId = @object.OfferedObjectId, Status = ObjectRegistrationStatus.OK, RecipientLoginId = user.Login.LoginId, ShouldReturnItAfter = shouldReturnItAfter, }; _registrationsRepo.Add(registrationModel); await _registrationsRepo.SaveChangesAsync(); var integrationEvent = new NewRegistrationIntegrationEvent() { Id = Guid.NewGuid(), OccuredAt = registrationModel.RegisteredAtUtc, ObjectId = @object.OriginalObjectId, RecipiantId = user.User.UserId.ToString(), ShouldReturn = @object.ShouldReturn, RegisteredAt = registrationModel.RegisteredAtUtc, RegistrationId = registrationModel.ObjectRegistrationId }; // Broadcast an event; _eventBus.Publish(integrationEvent); var token = await _tokenManager.GenerateToken(registrationModel.ObjectRegistrationId, TokenType.Receiving); var dto = new AddNewRegistrationResultDto { ObjectId = registrationModel.Object.OriginalObjectId, RegistrationId = registrationModel.ObjectRegistrationId, ShouldBeReturnedAfterReceving = registrationModel.ShouldReturnItAfter, RegistrationExpiresAtUtc = registrationModel.ExpiresAtUtc, RegistrationToken = new RegistrationTokenResultDto { RegistrationToken = token.Token, CreatedAtUtc = token.IssuedAtUtc, UseBeforeUtc = token.UseBeforeUtc } }; return(StatusCode(200, dto)); }
public async Task <IActionResult> Create([FromBody] AddNewRegistrationDto addnewRegistrationDto) { var result = await _registrationAdder.AddNewRegistrationAsync(addnewRegistrationDto); return(StatusCode(result)); }