Exemplo n.º 1
0
        public IActionResult Post([FromBody] CreateBusDto createBusDto)
        {
            try
            {
                var newBus = this._Mapper.Map <CreateBusDto, Bus>(createBusDto);
                if (this._BusRepository.SaveBus(newBus) > 0)
                {
                    return(Ok(
                               this._Mapper.Map <Bus, ReturnBusDto>(
                                   this._BusRepository.Busses.Where(o => o.ID == newBus.ID).First()
                                   )
                               ));
                }

                return(BadRequest(new BadRequestMessage
                {
                    Message = new string[] {
                        "Bus fails to create.",
                        "Tip: RegistrationNumber is already exists",
                        "ID of busmodel does not exist"
                    }
                }));
            }
            catch (Exception)
            {
                return(StatusCode(500, "Internal server error"));
            }
        }
Exemplo n.º 2
0
        public async Task <BusDto> CreateAsync(CreateBusDto createBusDto)
        {
            using (UnitOfWork)
            {
                var bus = new Bus(
                    createBusDto.Mark,
                    createBusDto.ExpeditionNumber,
                    createBusDto.SeatCount,
                    createBusDto.Route,
                    createBusDto.CompanyId, Guid.NewGuid());

                bus.AddBusDetail(new BusDetail()
                {
                    Color          = createBusDto.Color,
                    Plate          = createBusDto.Plate,
                    Km             = createBusDto.Km,
                    ProductionDate = createBusDto.ProductionDate
                });

                await _busRepository.CreateAsync(bus);

                await UnitOfWork.SaveChangesAsync();

                return(ObjectMapper.Map <Bus, BusDto>(bus));
            }
        }
Exemplo n.º 3
0
        public void CreateBus()
        {
            var target = new BussesController(this._EFBusRepository, this._MockMapper);

            var newBus = new CreateBusDto
            {
                RegistrationNumber = "TEST123",
                CapacityBoundary   = 10,
                SeatingPlace       = 15,
                StandingPlace      = 20,
                BusModelID         = 1
            };

            var result   = target.Post(newBus);
            var okResult = result as OkObjectResult;

            Assert.Equal(200, okResult.StatusCode);
            Assert.Equal(13, this._EFBusRepository.Busses.Count());
            Assert.Equal(20, this._EFBusRepository.Busses
                         .Where(o => o.RegistrationNumber == "TEST123").FirstOrDefault().StandingPlace);

            //ID of busmodel does not exist
            newBus = new CreateBusDto
            {
                RegistrationNumber = "TEST456",
                CapacityBoundary   = 10,
                SeatingPlace       = 15,
                StandingPlace      = 20,
                BusModelID         = 100
            };

            result = target.Post(newBus);
            var badResult = result as BadRequestObjectResult;

            Assert.Equal(400, badResult.StatusCode);

            //RegistrationNumber is already exists"
            newBus = new CreateBusDto
            {
                RegistrationNumber = "TEST123",
                CapacityBoundary   = 10,
                SeatingPlace       = 15,
                StandingPlace      = 20,
                BusModelID         = 1
            };

            result    = target.Post(newBus);
            badResult = result as BadRequestObjectResult;
            Assert.Equal(400, badResult.StatusCode);
        }