Ejemplo n.º 1
0
        public async Task <IActionResult> Add(PlacaEntity model)
        {
            PlacaBusiness placaBusiness = new PlacaBusiness();

            var response = placaBusiness.AddPlaca(_context, model);

            if (response.Error == false)
            {
                return(Ok(response));
            }
            else
            {
                return(BadRequest(response));
            }
        }
Ejemplo n.º 2
0
        public ResultResponse <PlacaResponse> AddPlaca(PapeletaVirtualDBContext _context, PlacaEntity model)
        {
            try
            {
                PlacaResponse placaResponseforAdd;
                ResultResponse <PlacaResponse> response = new ResultResponse <PlacaResponse>();
                if (model == null)
                {
                    response.Data    = null;
                    response.Error   = true;
                    response.Message = "Completar todos los campos";
                    return(response);
                }

                if (_context.Placa.Any(x => x.NumPlaca == model.NumPlaca))
                {
                    response.Data    = null;
                    response.Error   = true;
                    response.Message = "El número de Placa ya existe";
                    return(response);
                }
                else
                {
                    using (var ts = new TransactionScope()){
                        Models.Placa placa = new Models.Placa();
                        _context.Placa.Add(placa);
                        placa.CarBrand         = model.CarBrand;
                        placa.CarModel         = model.CarModel;
                        placa.TransportDetails = model.TransportDetails;
                        placa.NumPlaca         = model.NumPlaca;


                        _context.SaveChanges();
                        ts.Complete();
                        response.Data    = null;
                        response.Error   = false;
                        response.Message = "Placa registrada";
                    }
                    var result = _context.Placa.FirstOrDefault(x => x.NumPlaca == model.NumPlaca);
                    placaResponseforAdd = new PlacaResponse {
                        Id               = result.Id,
                        CarBrand         = result.CarBrand,
                        CarModel         = result.CarModel,
                        ReleaseDate      = result.ReleaseDate,
                        TransportDetails = result.TransportDetails,
                        NumPlaca         = result.NumPlaca
                    };
                    response.Data    = placaResponseforAdd;
                    response.Error   = false;
                    response.Message = "Placa registrada";
                }
                return(response);
            }
            catch (Exception ex)
            {
                throw new System.Exception(ex.Message);
            }
        }