/// <summary>
 /// Get Operations Work Place By Workplace Id
 /// </summary>
 public OperationWorkplaceSearchByIdResponse GetOperationsWorkPlaceByWorkplaceId(long workPlaceId)
 {
     return(new OperationWorkplaceSearchByIdResponse
     {
         OperationWorkPlaces = operationsWorkPlaceRepository.GetWorkPlaceOperationByWorkPlaceId(workPlaceId)
     });
 }
Esempio n. 2
0
        /// <summary>
        /// Save WorkPlace
        /// </summary>
        public WorkPlace SaveWorkPlace(WorkPlace workPlaceRequest)
        {
            // Check the availability of workplace code
            if (workplaceRepository.DoesWorkPlaceCodeExists(workPlaceRequest))
            {
                throw new CaresException(Resources.Organization.Workplace.WorkPlaceWithSameCodeExistsAlready);
            }

            WorkPlace dbWorkPlace         = workplaceRepository.Find(workPlaceRequest.WorkPlaceId);
            bool      isFind              = false;
            bool      isOperationInserted = false;

            #region Edit
            if (dbWorkPlace != null)
            {
                IEnumerable <OperationsWorkPlace> dBVersionOperationsWorkPlace = operationsWorkPlaceRepository.GetWorkPlaceOperationByWorkPlaceId(dbWorkPlace.WorkPlaceId);
                // Updating object properties
                UpdateWorkPlaceProperties(workPlaceRequest, dbWorkPlace);

                #region Operation already exists in DB
                // ReSharper disable once PossibleMultipleEnumeration
                if (dBVersionOperationsWorkPlace.Any())
                {
                    // check for every dBOperationsWorkPlace in DB
                    foreach (OperationsWorkPlace dBOperationsWorkPlace in dBVersionOperationsWorkPlace)
                    {
                        //If dBOperationsWorkPlace matches to any of request operation than dont delete it else delete
                        if (workPlaceRequest.OperationsWorkPlaces != null)
                        {
                            if (workPlaceRequest.OperationsWorkPlaces.Any(reqOpp => reqOpp.OperationsWorkPlaceId == dBOperationsWorkPlace.OperationsWorkPlaceId))
                            {
                                isFind = true;
                            }
                        }
                        else
                        {
                            DeleteOperation(dBOperationsWorkPlace.OperationsWorkPlaceId);
                        }
                        if (!isFind)
                        {
                            DeleteOperation(dBOperationsWorkPlace.OperationsWorkPlaceId);
                        }
                        isFind = false;
                    }
                }
                #endregion
                #region Adding New Operation
                else
                {
                    if (workPlaceRequest.OperationsWorkPlaces == null)
                    {
                        workPlaceRequest.OperationsWorkPlaces = new Collection <OperationsWorkPlace>();
                    }
                    foreach (var newOperation in workPlaceRequest.OperationsWorkPlaces)
                    {
                        newOperation.RecCreatedBy  = newOperation.RecLastUpdatedBy = operationRepository.LoggedInUserIdentity;
                        newOperation.RecCreatedDt  = newOperation.RecLastUpdatedDt = DateTime.Now;
                        newOperation.UserDomainKey = 1;
                        newOperation.IsActive      = true;
                        newOperation.IsDeleted     = false;
                        newOperation.IsPrivate     = false;
                        dbWorkPlace.OperationsWorkPlaces.Add(newOperation);
                        isOperationInserted = true;
                    }
                }
                #endregion
                #region Adding Operation when there are more Operation in request thatn in DB
                if (workPlaceRequest.OperationsWorkPlaces != null && (workPlaceRequest.OperationsWorkPlaces.Count > dBVersionOperationsWorkPlace.Count()) &&
                    isOperationInserted == false)
                {
                    foreach (var newOperation in workPlaceRequest.OperationsWorkPlaces)
                    {
                        if (newOperation.OperationsWorkPlaceId == 0)
                        {
                            newOperation.RecCreatedBy  = newOperation.RecLastUpdatedBy = operationRepository.LoggedInUserIdentity;
                            newOperation.RecCreatedDt  = newOperation.RecLastUpdatedDt = DateTime.Now;
                            newOperation.UserDomainKey = 1;
                            newOperation.IsActive      = true;
                            newOperation.IsDeleted     = false;
                            newOperation.IsPrivate     = false;
                            dbWorkPlace.OperationsWorkPlaces.Add(newOperation);
                        }
                    }
                }

                #endregion
                workplaceRepository.Update(dbWorkPlace);
            }
            #endregion
            #region ADD
            else
            {
                dbWorkPlace = workplaceRepository.Create();
                SetWorkPlaceProperties(workPlaceRequest, dbWorkPlace);
                if (workPlaceRequest.OperationsWorkPlaces != null)
                {
                    dbWorkPlace.OperationsWorkPlaces = new Collection <OperationsWorkPlace>();
                    foreach (var operation in (workPlaceRequest.OperationsWorkPlaces))
                    {
                        operation.RecCreatedBy  = operation.RecLastUpdatedBy = workplaceRepository.LoggedInUserIdentity;
                        operation.RecCreatedDt  = operation.RecLastUpdatedDt = DateTime.Now;
                        operation.UserDomainKey = 1;
                        operation.IsActive      = true;
                        operation.IsDeleted     = false;
                        operation.IsPrivate     = false;
                        dbWorkPlace.OperationsWorkPlaces.Add(operation);
                    }
                }
                workplaceRepository.Add(dbWorkPlace);
            }
            #endregion
            workplaceRepository.SaveChanges();
            //    operationsWorkPlaceRepository.SaveChanges();

            // To Load the proprties
            return(workplaceRepository.GetWorkplaceWithDetails(dbWorkPlace.WorkPlaceId));
        }