Ejemplo n.º 1
0
        public static async Task CreateOrFailAsnc(this ICarRepository repository, Car _car)
        {
            var car = await repository.GetAsync(_car.BrandName, _car.Model, _car.Generation);

            if (car == _car)
            {
                throw new ForbiddenValueException($"That car already exists.");
            }

            await repository.CreateAsync(_car);
        }
Ejemplo n.º 2
0
        public async Task <ReturnResult> PostCar(Car car)
        {
            await CarRepository.CreateAsync(car);

            await CarRepository.SaveChangesAsync();

            ReturnResult ReturnResult = new ReturnResult();

            ReturnResult.Action = "Carro criado";
            return(ReturnResult);
        }
Ejemplo n.º 3
0
        private async void SuccessfulAdd(object sender, EventArgs e)
        {
            var args = (EventEditCarArgs)e;
            var car  = args.Car;

            await carRepository.CreateAsync(car);

            var cars = await carRepository.GetAllAsync();

            Init(cars.ToList());
        }
Ejemplo n.º 4
0
 public async Task CreateAsync(CarDTO item)
 {
     if (item != null)
     {
         var car = _mapper.Map <CarDTO, Car>(item);
         await _carRepository.CreateAsync(car);
     }
     else
     {
         throw new Exception("Данные не заполнены");
     }
 }
        public async Task <IActionResult> Create([FromBody] Car car)
        {
            if (car == null)
            {
                return(BadRequest());
            }

            if (car.Serial == null ||
                car.Model == null ||
                car.Region == null)
            {
                return(BadRequest());
            }

            Car added = await carRepository.CreateAsync(car);

            return(CreatedAtRoute("GetCar", new { id = added.Id }, car));
        }
Ejemplo n.º 6
0
        public async Task Handle(CreateCarCommand message, CancellationToken cancellationToken)
        {
            var car = Car.Factory.NewCreate(message.Name, message.Price, message.Status, message.Tenant);

            if (!IsValid(car))
            {
                return;
            }

            //TODO:
            //Validação de Negócio
            //exemplo : Esse carro já existe com o mesmo nome?

            await _carRepository.CreateAsync(car);

            if (Commit())
            {
                Console.WriteLine("Car register with success.");
                _bus.RaiseEvent(new CreateCarEvent(car.ID, car.Name, car.Price, car.Status, car.Tenant));
            }
        }
Ejemplo n.º 7
0
        public async Task CreateAsync(CarDTO carDTO)
        {
            await _carValidator.CanCreateAsync(carDTO);

            await _carRepository.CreateAsync(carDTO);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Seeds data in the database
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseDatabaseMigrations <T>(this IApplicationBuilder app) where T : DbContext
        {
            using (IServiceScope serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                serviceScope.ServiceProvider.GetService <T>().Database.Migrate();

                RoleManager <IdentityRole <int> > roleManager = serviceScope.ServiceProvider.GetService <RoleManager <IdentityRole <int> > >();
                UserManager <User> userManager = serviceScope.ServiceProvider.GetService <UserManager <User> >();
                IPictureRepository pictureRepo = serviceScope.ServiceProvider.GetService <IPictureRepository>();
                ICarRepository     carRepo     = serviceScope.ServiceProvider.GetService <ICarRepository>();

                Task
                .Run(async() =>
                {
                    carRepo.SeedMakesAndModels();

                    string[] roles = new[]
                    {
                        CommonConstants.AdministratorRole,
                        CommonConstants.DriverRole
                    };

                    foreach (var role in roles)
                    {
                        bool roleExists = await roleManager.RoleExistsAsync(role);

                        if (!roleExists)
                        {
                            await roleManager.CreateAsync(new IdentityRole <int>
                            {
                                Name = role
                            });
                        }
                    }

                    // Users picture seed
                    string pictureName = "admin";
                    Picture picture    = await pictureRepo.GetByName(pictureName);

                    if (picture == null)     // Checks whether the admin picture exists
                    {
                        picture = new Picture
                        {
                            FileName = pictureName
                        };

                        await pictureRepo.InsertIntoDatabaseAsync(picture);
                    }

                    // Car picture seed
                    string carPictureName = "carSeed";
                    Picture carPicture    = await pictureRepo.GetByName(carPictureName);

                    if (carPicture == null)     // Checks whether the picture exists
                    {
                        carPicture = new Picture
                        {
                            FileName = carPictureName
                        };

                        await pictureRepo.InsertIntoDatabaseAsync(carPicture);
                    }

                    // Admin seed
                    string adminEmail    = "*****@*****.**";
                    string adminUsername = "******";
                    string adminPassword = "******";

                    User adminUser = await userManager.FindByNameAsync(adminUsername);

                    if (adminUser == null)
                    {
                        User user = new User
                        {
                            FirstName = "Admin",
                            LastName  = "Admin",
                            Email     = adminEmail,
                            UserName  = adminUsername,
                            ProfilePictureFileName = pictureName
                        };

                        await userManager.CreateAsync(user, adminPassword);
                        await userManager.AddToRoleAsync(user, CommonConstants.AdministratorRole);
                    }

                    // Driver seed
                    string driverEmail    = "*****@*****.**";
                    string driverUsername = "******";
                    string driverPassword = "******";

                    User driverUser = await userManager.FindByNameAsync(driverUsername);

                    if (driverUser == null)
                    {
                        driverUser = new User
                        {
                            FirstName = "Daniel",
                            LastName  = "Morales",
                            Email     = driverEmail,
                            UserName  = driverUsername,
                            ProfilePictureFileName = pictureName,
                            Cars = new List <Car>()
                        };

                        await userManager.CreateAsync(driverUser, driverPassword);
                        await userManager.AddToRoleAsync(driverUser, CommonConstants.DriverRole);
                    }

                    // Car seed
                    if (driverUser.Cars.Count() == 0)
                    {
                        await carRepo.CreateAsync(new Car
                        {
                            Colour          = "Червен",
                            ModelId         = 23,
                            OwnerId         = driverUser.Id,
                            PictureFileName = carPictureName
                        });
                    }

                    // Basic user seed
                    string basicUserEmail    = "*****@*****.**";
                    string basicUserUsername = "******";
                    string basicUserPassword = "******";

                    User basicUser = await userManager.FindByNameAsync(basicUserUsername);

                    if (basicUser == null)
                    {
                        User user = new User
                        {
                            FirstName = "John",
                            LastName  = "Doe",
                            Email     = basicUserEmail,
                            UserName  = basicUserUsername,
                            ProfilePictureFileName = pictureName
                        };

                        await userManager.CreateAsync(user, basicUserPassword);
                    }
                })
                .Wait();
            }
            return(app);
        }