示例#1
0
        public async Task <IActionResult> RegisterAsync([FromBody] RegisterVehicle command)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // insert vehicle
                    Vehicle vehicle = Mapper.Map <Vehicle>(command);
                    _dbContext.Vehicles.Add(vehicle);
                    await _dbContext.SaveChangesAsync();

                    //// send event.
                    //var e = Mapper.Map<VehicleRegistered>(command);
                    //await _messagePublisher.PublishMessageAsync(e.MessageType, e, "");

                    //return result
                    return(CreatedAtRoute("GetByCodigo", new { codigo = vehicle.Codigo }, vehicle));
                }
                return(BadRequest());
            }
            catch (Exception Ex)
            {
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
示例#2
0
        public async Task <IActionResult> RegisterAsync([FromBody] RegisterInsurance command)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // insert insurance
                    Insurance insurance = Mapper.Map <Insurance>(command);
                    insurance.Matricula = _dbContext.Vehicles.FirstOrDefaultAsync(x => x.Codigo == insurance.VehicleId).Result.Matricula;
                    _dbContext.Insurances.Add(insurance);
                    await _dbContext.SaveChangesAsync();

                    // send event
                    //var e = Mapper.Map<InsuranceRegistered>(command);
                    //await _messagePublisher.PublishMessageAsync(e.MessageType, e, "");

                    //return result
                    return(CreatedAtRoute("GetInsuranceById", new { insuranceId = insurance.InsuranceId }, insurance));
                }
                return(BadRequest());
            }
            catch (DbUpdateException)
            {
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
示例#3
0
        public async Task <IActionResult> RegisterAsync([FromBody] RegisterOwner command)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // insert owner
                    Owner owner = Mapper.Map <Owner>(command);
                    _dbContext.Owners.Add(owner);
                    await _dbContext.SaveChangesAsync();

                    // send event
                    //var e = Mapper.Map<OwnerRegistered>(command);
                    //await _messagePublisher.PublishMessageAsync(e.MessageType, e, "");

                    //return result
                    return(CreatedAtRoute("GetOwnerById", new { ownerID = owner.OwnerId }, owner));
                }
                return(BadRequest());
            }
            catch (Exception Ex)
            {
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
        public async Task <IActionResult> RegisterAsync([FromBody] RegisterVehicle command)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // check invariants
                    if (!Regex.IsMatch(command.LicenseNumber, NUMBER_PATTERN, RegexOptions.IgnoreCase))
                    {
                        return(BadRequest($"The specified license-number '{command.LicenseNumber}' was not in the correct format."));
                    }

                    // insert vehicle
                    Vehicle vehicle = command.MapToVehicle();
                    _dbContext.Vehicles.Add(vehicle);
                    await _dbContext.SaveChangesAsync();

                    // send event
                    var e = VehicleRegistered.FromCommand(command);
                    await _messagePublisher.PublishMessageAsync(e.MessageType, e, "");

                    //return result
                    return(CreatedAtRoute("GetByLicenseNumber", new { licenseNumber = vehicle.LicenseNumber }, vehicle));
                }
                return(BadRequest());
            }
            catch (DbUpdateException)
            {
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }