Example #1
0
        public void Edit(TruckModel truckModel)
        {
            try
            {
                Common.DAL.Truck truck = dbContext.Trucks.Where(x => x.Id == truckModel.Id).FirstOrDefault();

                if (truck == null)
                {
                    base.HandleError(truckModel, CommonLayer.LookUps.ErrorType.Critical, null, Resources.NotFound);
                    return;
                }

                if (Validate(truckModel))
                {
                    return;
                }

                TruckMapper.Map(dbContext, truckModel, truck);

                base.SaveChanges();

                truckModel.AddSuccess(Resources.TruckUpdatedSuccessfully, LookUps.SuccessType.Full);
            }
            catch (Exception ex)
            {
                base.HandleError(truckModel, CommonLayer.LookUps.ErrorType.Exception, ex);
                base.UndoUpdates();
            }
        }
Example #2
0
        public void Delete(TruckModel truckModel)
        {
            try
            {
                if (ValidateDelete(truckModel))
                {
                    return;
                }

                Common.DAL.Truck truck = dbContext.Trucks.Where(x => x.Id == truckModel.Id).FirstOrDefault();

                if (truck == null)
                {
                    base.HandleError(truckModel, CommonLayer.LookUps.ErrorType.Critical, null, Resources.NotFound);
                    return;
                }

                dbContext.Trucks.Remove(truck);

                base.SaveChanges();

                truckModel.AddSuccess(Resources.TruckDeletedSuccessfully, LookUps.SuccessType.Full);
            }
            catch (System.Data.Entity.Infrastructure.DbUpdateException)
            {
                base.HandleError(truckModel, CommonLayer.LookUps.ErrorType.Business, null, Resources.RefrenceDeleteError);
                base.UndoUpdates();
            }
            catch (Exception ex)
            {
                base.HandleError(truckModel, CommonLayer.LookUps.ErrorType.Exception, ex);
                base.UndoUpdates();
            }
        }
Example #3
0
        public void Create(TruckModel truckModel)
        {
            try
            {
                if (Validate(truckModel))
                {
                    return;
                }

                using (var transaction = dbContext.Database.BeginTransaction())
                {
                    try
                    {
                        Common.DAL.Truck truck = new Common.DAL.Truck();
                        TruckMapper.Map(dbContext, truckModel, truck);

                        truck.Id = Guid.NewGuid().ToString();

                        dbContext.Trucks.Add(truck);

                        base.SaveChanges();
                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw ex;
                    }

                    truckModel.AddSuccess(Resources.TruckAddedSuccessfully, LookUps.SuccessType.Full);
                }
            }
            catch (Exception ex)
            {
                base.HandleError(truckModel, CommonLayer.LookUps.ErrorType.Exception, ex);
                base.UndoUpdates();
            }
        }