/// <summary>
        /// Creates a new reservation and saves it to the database
        /// </summary>
        /// <param name="request">Create reservation DTO</param>
        /// <returns>The newly created reserrvation DTO</returns>
        public Dto.ReservationResult AddNewReservation(Dto.ReservationRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            return(Call(() =>
            {
                //creates a new reservation entity
                var reservation = ReservationFactory.CreateReservation(request.Name, request.ReservationDateTime, request.GuestsCount, request.UserId);

                var entityValidator = EntityValidatorLocator.CreateValidator();
                if (entityValidator.IsValid(reservation))
                {
                    using (var transaction = _reservationRepository.BeginTransaction())
                    {
                        _reservationRepository.Add(reservation);
                        transaction.Commit();
                    }
                }
                else
                {
                    return new Dto.ReservationResult
                    {
                        Status = ActionResultCode.Errored,
                        Message = Messages.validation_errors,
                        Errors = entityValidator.GetInvalidMessages(reservation)
                    };
                }

                //returns success
                return reservation.ProjectedAs <Dto.ReservationResult>();
            }));
        }
        /// <summary>
        /// Updates an existing reservation
        /// </summary>
        /// <param name="id">The reservation id</param>
        /// <param name="request">The changed reservation DTO</param>
        /// <returns>The updated reservation DTO</returns>
        public Dto.ReservationResult UpdateReservation(int id, Dto.ReservationRequest request)
        {
            if (id < 1)
            {
                throw new ArgumentOutOfRangeException("id");
            }

            return(Call(() =>
            {
                using (var transaction = _reservationRepository.BeginTransaction())
                {
                    //get the reservation
                    var reservation = _reservationRepository.Get(id);

                    //returns failure if a reservation is not found
                    if (reservation == null)
                    {
                        return new Dto.ReservationResult
                        {
                            Status = ActionResultCode.Failed,
                            Message = string.Format(Messages.reservation_not_found, id)
                        };
                    }

                    //updates fields from the DTO
                    reservation.Name = request.Name;
                    reservation.ReservationDateTime = request.ReservationDateTime;
                    reservation.GuestsCount = request.GuestsCount;
                    reservation.ModifiedBy = request.UserId;
                    reservation.ModifiedOn = DateTime.Now;

                    //validates the reservation
                    var entityValidator = EntityValidatorLocator.CreateValidator();
                    if (entityValidator.IsValid(reservation))
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        return new Dto.ReservationResult
                        {
                            Status = ActionResultCode.Errored,
                            Message = Messages.validation_errors,
                            Errors = entityValidator.GetInvalidMessages(reservation)
                        };
                    }

                    //returns the updated reservation
                    return reservation.ProjectedAs <Dto.ReservationResult>();
                }
            }));
        }
        public static void AddBuildingBlocks(this IServiceCollection services, params Assembly[] autoMapperAssemblies)
        {
            services.AddTransient(typeof(IPipelineBehavior <,>), typeof(ValidatorBehavior <,>));
            services.AddTransient(typeof(IPipelineBehavior <,>), typeof(LoggingBehavior <,>));

            services.TryAddTransient <IDomainMediator, MediatRAdapter>();

            services.AddTransient <IEntityValidatorFactory, DataAnnotationsEntityValidatorFactory>();

            services.AddAutoMapper(autoMapperAssemblies);

            //sigletons
            TypeAdapterLocator.SetCurrent(new AutoMapperTypeAdapterFactory(autoMapperAssemblies));
            EntityValidatorLocator.SetCurrent(new DataAnnotationsEntityValidatorFactory());
        }
Ejemplo n.º 4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.TryAddTransient <IHttpContextAccessor, HttpContextAccessor>();

            services.AddCors();
            services.AddSecurity(Configuration);

            services.AddDbContext <CarRentalUnitOfWork>(optionsBuilder => {
                optionsBuilder
                .UseMySQL(Configuration.GetConnectionString("CarRentalDataSource"));
            }, ServiceLifetime.Scoped);

            // registers repositories
            services.AddTransient <ReadModel.IAvailableCarRepository, Infrastructure.Repositories.AvailableCarRepository>();
            services.AddTransient <IAvailableCarRepository, AvailableCarRepository>();
            services.AddTransient <IUserRepository, UserRepository>();
            services.AddTransient <IBookingRepository, BookingRepository>();

            //register application services
            services.AddTransient <IAvailableCarAppService, AvailableCarAppService>();

            services.AddTransient <IAvailableCarService, AvailableCarService>();

            services.AddMediatR(typeof(CreateBookingCommand).Assembly);
            services.AddAutoMapper();

            services.AddMvc()
            .AddFluentValidation(fv => {
                fv.ImplicitlyValidateChildProperties = true;
                fv.RegisterValidatorsFromAssemblyContaining <AddressModel>();
                fv.RegisterValidatorsFromAssemblyContaining <CreateBookingCommand>();
            });

            //sigletos
            TypeAdapterLocator.SetCurrent(new AutoMapperTypeAdapterFactory());
            EntityValidatorLocator.SetCurrent(new DataAnnotationsEntityValidatorFactory());
            LoggerLocator.SetCurrent(new Log4NetLoggerFactory());
        }
        public static EntityResult <T> SaveEntity <TEntity, T>(this IRepository <TEntity> repository, T entity)
            where TEntity : class
            where T : class, TEntity
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            var entityValidator = EntityValidatorLocator.CreateValidator();
            var isValid         = entityValidator.IsValid(entity);

            if (!isValid)
            {
                return(new EntityResult <T>(entityValidator, entity, false));
            }

            var context = repository.UnitOfWork;

            repository.Add(entity);
            context.SaveChanges();

            return(new EntityResult <T>(entityValidator, entity, true));
        }
        public static EntityResult <T> SaveEntity <TEntity, T>(this IRepository <TEntity> repository, T entity)
            where TEntity : class
            where T : class, TEntity
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            var entityValidator = EntityValidatorLocator.CreateValidator();
            var isValid         = entityValidator.IsValid(entity);

            if (!isValid)
            {
                return(new EntityResult <T>(entityValidator, entity, false));
            }

            using (var transaction = repository.BeginTransaction())
            {
                repository.Add(entity);
                transaction.Commit();
            }
            return(new EntityResult <T>(entityValidator, entity, true));
        }
Ejemplo n.º 7
0
 private static void RegisterSingletons()
 {
     TypeAdapterLocator.SetCurrent(new AutoMapperTypeAdapterFactory());
     EntityValidatorLocator.SetCurrent(new DataAnnotationsEntityValidatorFactory());
     LoggerLocator.SetCurrent(new Log4NetLoggerFactory());
 }