public void UpdateParcel(DTO.Parcel updatedParcel) { CheckHelper.ArgumentNotNull(updatedParcel, "updatedParcel"); CheckHelper.ArgumentWithinCondition(!updatedParcel.IsNew(), "Parcel is new."); Container.Get <IValidateService>().CheckIsValid(updatedParcel); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition( SecurityService.IsCurrentUserPurchaser || SecurityService.IsCurrentUserDistributor, "Only purchaser and distributor can change parcel."); var persistentService = Container.Get <IPersistentService>(); var parcel = persistentService.GetEntityById <DataAccess.Parcel>(updatedParcel.Id); CheckHelper.NotNull(parcel, "Parcel does not exist."); CheckHelper.WithinCondition( SecurityService.IsCurrentUserPurchaser || IsCurrentUserDistributorForParcel(parcel), "Only purchaser and distributor can change parcel."); if (IsCurrentUserDistributorForParcel(parcel)) { parcel.ReceivedDate = updatedParcel.ReceivedDate; } else { updatedParcel.ReceivedDate = parcel.ReceivedDate; } if (SecurityService.IsCurrentUserPurchaser) { parcel.RublesPerDollar = updatedParcel.RublesPerDollar; parcel.PurchaserSpentOnDelivery = updatedParcel.PurchaserSpentOnDelivery; parcel.TrackingNumber = updatedParcel.TrackingNumber; parcel.SentDate = updatedParcel.SentDate; parcel.Comments = updatedParcel.Comments; UpdateDistributor(parcel, updatedParcel.Distributor); } else { updatedParcel.RublesPerDollar = parcel.RublesPerDollar; updatedParcel.PurchaserSpentOnDelivery = parcel.PurchaserSpentOnDelivery; updatedParcel.TrackingNumber = parcel.TrackingNumber; updatedParcel.SentDate = parcel.SentDate; updatedParcel.Comments = parcel.Comments; updatedParcel.Distributor = parcel.Distributor == null ? null : Container.Get <IDtoService>().CreateUser(parcel.Distributor); } parcel.UpdateTrackFields(Container); persistentService.SaveChanges(); }
public DTO.Order[] GetOrdersByParcel(DTO.Parcel parcel, string filter) { CheckHelper.ArgumentNotNull(parcel, "parcel"); CheckHelper.ArgumentWithinCondition(!parcel.IsNew(), "Parcel is new."); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition( SecurityService.IsCurrentUserSeller || SecurityService.IsCurrentUserPurchaser || SecurityService.IsCurrentUserDistributor, "Only seller, purchaser and distributor can get all orders."); var parcelId = parcel.Id; var currentParcel = Container .Get <IPersistentService>() .GetEntityById <DataAccess.Parcel>(parcelId); CheckHelper.NotNull(currentParcel, "Parcel does not exist."); var currentUserId = SecurityService.CurrentUser.Id; CheckHelper.WithinCondition( SecurityService.IsCurrentUserPurchaser || (SecurityService.IsCurrentUserDistributor && currentParcel.Distributor != null && currentParcel.DistributorId == currentUserId) || (SecurityService.IsCurrentUserSeller && currentParcel.Orders.Any(o => o.CreateUserId == currentUserId)), "Current user is either distributor and parcel is not assigned to him or seller and parcel does not have any order created by him."); var query = Container .Get <IPersistentService>() .GetEntitySet <DataAccess.Order>() .Where(o => o.Parcel != null && o.ParcelId == parcelId); ApplyOrderFilterCondition(ref query, filter); Func <DataAccess.Order, bool> predicate = null; if (SecurityService.IsCurrentUserSeller && !SecurityService.IsCurrentUserPurchaser && !(SecurityService.IsCurrentUserDistributor && currentParcel.Distributor != null && currentParcel.DistributorId == currentUserId)) { query = query.Where(o => o.CreateUserId == currentUserId); predicate = o => o.CreateUserId == currentUserId; } var dtoService = Container.Get <IDtoService>(); return (query .OrderBy(o => o.Id) .AsEnumerable() .Select(o => dtoService.CreateOrder(o, false, predicate)) .ToArray()); }
public void CreateParcel(DTO.Parcel createdParcel) { CheckHelper.ArgumentNotNull(createdParcel, "createdParcel"); CheckHelper.ArgumentWithinCondition(createdParcel.IsNew(), "Parcel is not new."); Container.Get <IValidateService>().CheckIsValid(createdParcel); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition(SecurityService.IsCurrentUserPurchaser, "Only purchaser can create parcel."); var persistentService = Container.Get <IPersistentService>(); var parcel = new DataAccess.Parcel { RublesPerDollar = createdParcel.RublesPerDollar, PurchaserSpentOnDelivery = createdParcel.PurchaserSpentOnDelivery, TrackingNumber = createdParcel.TrackingNumber, SentDate = createdParcel.SentDate, Comments = createdParcel.Comments }; parcel.UpdateTrackFields(Container); UpdateDistributor(parcel, createdParcel.Distributor); persistentService.Add(parcel); persistentService.SaveChanges(); createdParcel.Id = parcel.Id; createdParcel.CreateDate = parcel.CreateDate; createdParcel.CreateUser = parcel.CreatedBy.GetFullName(); createdParcel.ChangeDate = parcel.ChangeDate; createdParcel.ChangeUser = parcel.ChangedBy.GetFullName(); createdParcel.ReceivedDate = null; }