Ejemplo n.º 1
0
        public ActionResult Add(OrganizationAddViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Organization organization          = Organization.Empty(viewModel.Name, viewModel.OrganizationTypeId);
                var          organizationRelations = _organizationRepository.GetOrganizationRelation(viewModel.OrganizationId);
                foreach (var organizationRelation in organizationRelations)
                {
                    organization.OrganizationRelations.Add(
                        new OrganizationRelation
                    {
                        Level = (byte)(organizationRelation.Level + 1),
                        Order = (byte)(organizationRelation.Order + 1),
                        RelatedOrganizationId = organizationRelation.RelatedOrganizationId
                    }
                        );
                }
                organization.OrganizationRelations.Add(new OrganizationRelation {
                    Level = 1, Order = 1, RelatedOrganization = organization
                });
                EntityContext.AddEntityProgress(
                    organization.EntityInfo,
                    new EntityProgress
                {
                    Organization       = organization,
                    EntityStateAfterId = Dom.EntityType.Organization.State.Created
                }
                    );
                EntityContext.AddEntityOrganization(
                    organization.EntityInfo,
                    organization,
                    Dom.EntityType.Organization.State.Created);
                _organizationRepository.AddOrUpdate(organization);
                _organizationRepository.UnitOfWork.SaveChanges();

                return(RedirectToAction(Mvc.Controller.Organization.List, Mvc.Controller.Organization.Name));
            }

            ICollection <SelectListItemCount> userOrganizations = _organizationRepository.GetOrganizations(
                UserContext.User.Id,
                UserContext.User.OrganizationId,
                new List <int> {
                viewModel.OrganizationTypeId - 1
            }
                );

            viewModel.OrganizationTypeItems = InitOrganizationTypeSelectedListItems();
            viewModel.OrganizationItems     = Mapper.Map <ICollection <SelectListItem> >(userOrganizations);
            return(View(Mvc.View.Organization.Add, viewModel));
        }