public async Task<bool> Update(Vehicle item)
        {
            var veh = await IdExist(item.Id);

            veh.IdCar = await IdCarExist(item.IdCar);
            veh.HdId = await HdExist(item.HdId);

            if (item.LicensePlate != null)
                veh.LicensePlate = await LpExist(item.LicensePlate);
            if (item.LicensePlateType != null)
                veh.LicensePlateType = await LpTypeExist(item.LicensePlateType.Value);
            if (item.LicensePlateAt != null)
                veh.LicensePlateAt = await LpAtExist(item.LicensePlateAt.Value);
            if (item.ModelCarId != null)
                veh.ModelCarId = await ModelExist(item.ModelCarId.Value);
            if (item.ColorCarId != null)
                veh.ColorCarId = await ColorExist(item.ColorCarId.Value);
            if (item.OganizeCarId != null)
                veh.OganizeCarId = await OgnExist(item.OganizeCarId.Value);
            if (item.BodyNo != null)
                veh.BodyNo = await BodyNoExist(item.BodyNo);

            _db.Entry(veh).State = EntityState.Modified;
            try
            {
                await _db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException exception)
            {
                throw new DbUpdateConcurrencyException(exception.Message);
            }

            return true;
        }
        public async Task<Vehicle> Add(Vehicle item)
        {
            var newVeh = new Vehicle
            {
                IdCar = await IdCarExist(item.IdCar),
                HdId = await HdExist(item.HdId)
            };

            if (item.LicensePlate != null) 
                newVeh.LicensePlate = await LpExist(item.LicensePlate);
            if (item.LicensePlateType != null) 
                newVeh.LicensePlateType = await LpTypeExist(item.LicensePlateType.Value);
            if (item.LicensePlateAt != null) 
                newVeh.LicensePlateAt = await LpAtExist(item.LicensePlateAt.Value);
            if (item.ModelCarId != null) 
                newVeh.ModelCarId = await ModelExist(item.ModelCarId.Value);
            if (item.ColorCarId != null) 
                newVeh.ColorCarId = await ColorExist(item.ColorCarId.Value);
            if (item.OganizeCarId != null) 
                newVeh.OganizeCarId = await OgnExist(item.OganizeCarId.Value);
            if (item.BodyNo != null) 
                newVeh.BodyNo = await BodyNoExist(item.BodyNo);

            newVeh = _db.Vehicles.Add(newVeh);
            try
            {
                await _db.SaveChangesAsync();

                //add universe
                var newUn = new Universe
                {
                    VehicleId = newVeh.Id,
                    DisplayStatus = 2
                };
                _db.Universes.Add(newUn);
                try
                {
                    await _db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException exception)
                {
                    throw new DbUpdateConcurrencyException(exception.Message);
                }

                return newVeh;
            }
            catch (DbUpdateConcurrencyException exception)
            {
                throw new DbUpdateConcurrencyException(exception.Message);
            }
        }
        public async Task<IHttpActionResult> Post([FromUri]short hdId, [FromUri]int q) //q = quantity
        {
            if (hdId <= 0 || q <= 0)
                return BadRequest();

            try
            {
                var maxIdCar = _repository.GetAll().Where(c => c.HdId == hdId).Max(c => c.IdCar);
                if (maxIdCar == null) throw new ArgumentNullException("maxIdCar");

                for (var i = 0; i < q; i++)
                {
                    var max = Convert.ToInt32(maxIdCar) + 1;

                    var newVeh = new Vehicle
                    {
                        IdCar = max.ToString("D6"),
                        HdId = hdId
                    };

                    await _repository.Add(newVeh);
                }

                return Ok();
            }
            catch (ArgumentException msgArgumentException)
            {
                return BadRequest(msgArgumentException.Message);
            }
            catch (DbUpdateException msgDbUpdateException)
            {
                return InternalServerError(msgDbUpdateException);
            }
        }
Esempio n. 4
0
        private async Task<bool> AddVehicle(int quantity,short hdId,int orderId)
        {
            var car = _db.Vehicles.Where(c => c.HdId == hdId);//.Max(c => c.IdCar);

            //New HD and New Vehicle ... start at "000001"
            var maxIdCar = "0";
            //add New vehicle to current hd ... start at max idcar + 1
            if (car.Any()) maxIdCar = car.Max(c => c.IdCar);

            var max = Convert.ToInt32(maxIdCar);

            for (var i = 0; i < quantity; i++)
            {
                max = max + 1;

                var newVeh = new Vehicle
                {
                    IdCar = max.ToString("D6"),
                    HdId = hdId
                };

                newVeh = _db.Vehicles.Add(newVeh);
                try
                {
                    await _db.SaveChangesAsync();

                    //add universe
                    var newUn = new Universe
                    {
                        VehicleId = newVeh.Id,
                        OrderId = orderId,
                        DisplayStatus = 2//test
                    };
                    _db.Universes.Add(newUn);
                    try
                    {
                        await _db.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException exception)
                    {
                        throw new DbUpdateConcurrencyException(exception.Message);
                    }
                }
                catch (DbUpdateConcurrencyException exception)
                {
                    throw new DbUpdateConcurrencyException(exception.Message);
                }
            }

            return true;
        }