public async Task <IActionResult> Post([FromBody] CreatePilotModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var id = Guid.NewGuid();

            var command = new CreatePilotCommand
            {
                Id          = id,
                DateOfBirth = model.DateOfBirth,
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                Experience  = model.Experience
            };

            await _commandBus.ExecuteAsync(command);

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetCrewsFromAPI()
        {
            var client   = new HttpClient();
            var fileName = DateTime.UtcNow + "_log_date_time.csv";

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            string json = await client.GetStringAsync("http://5b128555d50a5c0014ef1204.mockapi.io/crew");

            List <TenCrewsModel> data    = JsonConvert.DeserializeObject <List <TenCrewsModel> >(json);
            List <TenCrewsModel> tenCrew = data.Where(c => c.Id < 11).ToList();

            //Changing int Id to Guid because my DB is working with them.
            var crewTasks = new List <Task>();

            foreach (var crew in tenCrew)
            {
                var StewardessesId = new List <Guid>();
                var stTasks        = new List <Task>();
                foreach (var stewardess in crew.Stewardess)
                {
                    var stewardessId = Guid.NewGuid();
                    var c            = new CreateStewardessCommand
                    {
                        Id          = stewardessId,
                        DateOfBirth = stewardess.BirthDate,
                        FirstName   = stewardess.FirstName,
                        LastName    = stewardess.LastName
                    };
                    stTasks.Add(_commandBus.ExecuteAsync(c));

                    StewardessesId.Add(stewardessId);
                }
                await Task.WhenAll(stTasks);

                var pilot   = crew.Pilot.First();
                var pilotId = Guid.NewGuid();
                var command = new CreatePilotCommand
                {
                    Id          = pilotId,
                    FirstName   = pilot.FirstName,
                    LastName    = pilot.LastName,
                    Experience  = pilot.Exp,
                    DateOfBirth = pilot.BirthDate
                };

                await _commandBus.ExecuteAsync(command);


                var createCrewCommand = new CreateCrewCommand
                {
                    Id              = Guid.NewGuid(),
                    PilotId         = pilotId,
                    StewardressesId = StewardessesId
                };

                crewTasks.Add(_commandBus.ExecuteAsync(createCrewCommand));
            }
            crewTasks.Add(LogCrews(tenCrew, Path.Combine(Environment.CurrentDirectory, @"Logs\", fileName)));
            await Task.WhenAll(crewTasks);

            return(Ok(json));
        }