Base solution for your next web application
Open Closed

Extending Organization Unit #4973


User avatar
0
cyklussoftware created

I am using ASP.NET Core v5.3 jQuery.

I am trying to extend the OrganizationUnit entity to include a boolean called IsOrganizer. I followed the Non-Abstract part of the Extending Entities tutorial to the best of my ability, but ultimately I couldn't follow it for various reasons (not applicable to OrganizationUnit, seems old or for angular version, etc).

So, does anyone have any guidance?

The things I did so far:

  • Created a new class called ExtendedOrganizationUnit (for lack of a better name) that inherits from OrganizationUnit

  • Added the new DbSet for ExtendedOrganizationUnit to the DbContext.

  • Added the added a migration

  • Updated the database (saw the change successfully)

  • Edited the OrganizationUnitDto to include my IsOrganizer boolean.

  • Edited the CreateOrganizationUnitInput to include my IsOrganizer boolean

  • Edited the UpdateOrganizationUnitInput to include my IsOrganizer boolean

  • I added a check-box input control to the _CreateModal and _EditModals

Now, I know I need to do something with the CreateOrganizationUnit and UpdateOrganizationUnit methods of the OrganizationUnitAppService. I'm not sure if I need to do anything to the GetOrganizationUnits method.

I think I also need to do something with the following:

  • CustomDtoMapper, but I'm not sure what to add / remove.
  • EditOrganizationUnitModalViewModel - I need to add the IsOrganizer bool and an AutoMapFrom setting.
  • OrganizationUnitsController - I think I need to case the OrganizationUnit to my custom variant.

What am I missing? Is there anything that I DIDN'T need to change?


7 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    hi,

    You need to add IsOrganizer property to CreateOrganizationUnitInput, OrganizationUnitDto, EditOrganizationUnitModalViewModel classes. You need to create a new mapping (in CustomDtoMapper) for ExtendedOrganizationUnit class like below

    configuration.CreateMap<ExtendedOrganizationUnit , OrganizationUnitDto>().ReverseMap();
    

    Change automap attribute to be mapped from ExtendedOrganizationUnit

    [AutoMapFrom(typeof(ExtendedOrganizationUnit ))]
        public class EditOrganizationUnitModalViewModel
        {
            public long? Id { get; set; }
    
            public string DisplayName { get; set; }
    
            public bool IsOrganizer  {get;set;}
        }
    

    In OrganizationUnitsController , change EditModal method to get from ExtendedOrganizationUnitRepository (you need to inject this repository)

    //....
      private readonly IRepository<OrganizationUnit, long> _extendedOrganizationUnitRepository;
    //....
    
    public async Task<PartialViewResult> EditModal(long id)
    {
    //....
         var organizationUnit = await _extendedOrganizationUnitRepository.GetAsync(id);
    //....
    }
    
    • No need to anything for GetOrganizationUnits method.
  • User Avatar
    0
    cyklussoftware created

    Great! Thanks for the detailed answer.

    I am getting an error when trying to create a new OrganizationUnit with the _CreateModal.cshtml (with my added check-box)

    The error says "Your request is not valid! The following errors were detected during validation. -"

    Is there some javascript file that I need to change?

  • User Avatar
    0
    cyklussoftware created

    Turns out this is only a problem if the CheckBox is checked. If it is unchecked, the form saves correctly.

  • User Avatar
    0
    cyklussoftware created

    Alright, sorry for the large number of posts, but I got it to work. Turns out the checkbox was returning the value of "on/off" instead of "true/false" which threw everything off.

    In my _CreateModal I added the 'value' attribute to the checkbox and set 'value="true"'. Setting the value to true doesn't set the checkbox to checked/unchecked, but I guess it triggers it to return true/false instead of on/off.

    Then in my _EditModal, I added an @Html.EditorFor(x => x.IsOrganizer). Now the checkbox is properly checked and unchecked and returns the proper true/false value.

  • User Avatar
    0
    aaron created
    Support Team

    That's great :)

    Setting the value to true doesn't set the checkbox to checked/unchecked, but I guess it triggers it to return true/false instead of on/off.

    Correct.

  • User Avatar
    0
    direccionti created

    Hello! I have a problem when get all organization units.

    My Custom class:

    public class OrganizationUnitExtended: OrganizationUnit
        {
            public virtual int TipoUnidadOrganizacionalId { get; set; }
    
            [ForeignKey("TipoUnidadOrganizacionalId")]
            public virtual TipoUnidadOrganizacional TipoUnidadOrganizacionalFk { get; set; }
    
            public virtual int MunicipioId { get; set; }
    
            [ForeignKey("MunicipioId")]
            public virtual Municipio MunicipioFk { get; set; }
        }
    

    Dto: the two fields are in the necessary dto.

    public class OrganizationUnitDto : AuditedEntityDto<long>
        {
            public long? ParentId { get; set; }
    
            public string Code { get; set; }
    
            public string DisplayName { get; set; }
    
            public int MemberCount { get; set; }
            
            public int RoleCount { get; set; }
    
            public int TipoUnidadOrganizacionalId { get; set; }
    
            public int MunicipioId { get; set; }
        }
    

    DbContext:

    public virtual DbSet<OrganizationUnitExtended> OrganizationUnitsExtended { get; set; }
    

    Mapper:

    //OrganizationUnit
                configuration.CreateMap<OrganizationUnit, OrganizationUnitDto>();
                configuration.CreateMap<OrganizationUnitExtended, OrganizationUnitDto>().ReverseMap();
    

    Service:

    Defintion:

    private readonly IRepository<OrganizationUnitExtended, long> _lookup_organizationUnitExtendedRepository;
    
    public CommonLookupAppService(
    ...
    IRepository<OrganizationUnitExtended, long> lookup_organizationUnitExtendedRepository,
    ...
    ){
    _lookup_organizationUnitExtendedRepository = lookup_organizationUnitExtendedRepository;
    ....
    }
    
    

    Method: I want to get all organization units for the new field (TipoUnidadOrganizacional)

    public async Task<PagedResultDto<OrganizationUnitLookupTableDto>> GetAllOrganizationUnitForLookupTable(GetAllForLookupTableInput input)
            {
                long currentUserId = Convert.ToInt64(AbpSession.UserId);
    
                var query = (from uou in _userOrganizationUnitRepository.GetAll()
                             join ou in _lookup_organizationUnitExtendedRepository.GetAll() on uou.OrganizationUnitId equals ou.Id
                             where uou.UserId == currentUserId && ou.TipoUnidadOrganizacionalId == (int)eTiposUnidadesOrganizacionales.Vitrina
                             select ou);
    
                var totalCount = await query.CountAsync();
    
                var organizationUnitList = await query
                    .PageBy(input)
                    .ToListAsync();
    
                var lookupTableDtoList = new List<OrganizationUnitLookupTableDto>();
                foreach (var organizationUnit in organizationUnitList)
                {
                    lookupTableDtoList.Add(new OrganizationUnitLookupTableDto
                    {
                        Id = organizationUnit.Id,
                        DisplayName = organizationUnit.DisplayName?.ToString()
                    });
                }
    
                return new PagedResultDto<OrganizationUnitLookupTableDto>(
                    totalCount,
                    lookupTableDtoList
                );
            }
    

    My error:

    "Invalid column name 'Discriminator'.\r\nInvalid column name 'Discriminator'."

    I need help! thanks!

  • User Avatar
    0
    direccionti created

    I got it!

    In the dbContext, in "OnModelCreatring" method i added:

    modelBuilder.Entity<OrganizationUnit>(b =>
                {
                    b.Property<string>("Discriminator")
                        .IsRequired();
                    b.ToTable("AbpOrganizationUnits");
                    b.HasDiscriminator<string>("Discriminator").HasValue("OrganizationUnit");
                });
    
                modelBuilder.Entity<OrganizationUnitExtended>(b =>
                {
                    b.HasBaseType("Abp.Organizations.OrganizationUnit");
                    b.Property<int>("MunicipioId");
                    b.Property<int>("TipoUnidadOrganizacionalId");
                    b.HasIndex("MunicipioId");
                    b.HasIndex("TipoUnidadOrganizacionalId");
                    b.ToTable("AbpOrganizationUnits");
                    b.HasDiscriminator().HasValue("OrganizationUnitExtended");
                });