public void InsertVehicle(VehicleViewModel vehicle, DateTime expirationDate, List <VehicleWheelViewModel> vehicleWheels, int userId) { using (var trans = _unitOfWork.BeginTransaction()) { try { DateTime serverTime = DateTime.Now; Vehicle entity = new Vehicle(); Map(vehicle, entity); entity.CreateDate = entity.ModifyDate = serverTime; entity.CreateUserId = entity.ModifyUserId = userId; entity.Status = (int)DbConstant.DefaultDataStatus.Active; _vehicleRepository.AttachNavigation(entity.Brand); _vehicleRepository.AttachNavigation(entity.Type); _vehicleRepository.AttachNavigation(entity.VehicleGroup); _vehicleRepository.AttachNavigation(entity.Customer); _vehicleRepository.AttachNavigation(entity.CreateUser); _vehicleRepository.AttachNavigation(entity.ModifyUser); var insertedVehicle = _vehicleRepository.Add(entity); _unitOfWork.SaveChanges(); VehicleDetail vehicleDetail = new VehicleDetail { VehicleId = insertedVehicle.Id, LicenseNumber = insertedVehicle.ActiveLicenseNumber, ExpirationDate = expirationDate, CreateDate = serverTime, ModifyDate = serverTime, ModifyUserId = userId, CreateUserId = userId, Status = (int)DbConstant.LicenseNumberStatus.Active }; _vehicleDetailRepository.AttachNavigation(vehicleDetail.Vehicle); _vehicleDetailRepository.AttachNavigation(vehicleDetail.CreateUser); _vehicleDetailRepository.AttachNavigation(vehicleDetail.ModifyUser); _vehicleDetailRepository.Add(vehicleDetail); _unitOfWork.SaveChanges(); _unitOfWork.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw; } } }
public void UpdateVehicleDetail(VehicleDetailViewModel vehicleDetail, VehicleViewModel vehicle, int userId) { DateTime serverTime = DateTime.Now; //set current active detail to expired if any VehicleDetail toBeExpired = _vehicleDetailRepository.GetMany(vd => vd.Status == (int)DbConstant.LicenseNumberStatus.Active).FirstOrDefault(); if (toBeExpired != null) { toBeExpired.ModifyDate = serverTime; toBeExpired.ModifyUserId = userId; toBeExpired.Status = (int)DbConstant.LicenseNumberStatus.Expired; _vehicleDetailRepository.AttachNavigation <Vehicle>(toBeExpired.Vehicle); _vehicleDetailRepository.Update(toBeExpired); _unitOfWork.SaveChanges(); } //insert new detail as active detail vehicleDetail.CreateDate = serverTime; vehicleDetail.CreateUserId = userId; vehicleDetail.ModifyUserId = userId; vehicleDetail.VehicleId = vehicle.Id; vehicleDetail.Status = (int)DbConstant.LicenseNumberStatus.Active; VehicleDetail entityDetail = new VehicleDetail(); Map(vehicleDetail, entityDetail); _vehicleDetailRepository.AttachNavigation <Vehicle>(entityDetail.Vehicle); _vehicleDetailRepository.Add(entityDetail); _unitOfWork.SaveChanges(); //update active license number in vehicle if (vehicle != null) { vehicle.ModifyDate = serverTime; vehicle.ModifyUserId = userId; vehicle.ActiveLicenseNumber = vehicleDetail.LicenseNumber; Vehicle entity = _vehicleRepository.GetById(vehicle.Id); Map(vehicle, entity); _vehicleRepository.AttachNavigation <Customer>(entity.Customer); _vehicleRepository.Update(entity); _unitOfWork.SaveChanges(); } _unitOfWork.SaveChanges(); }