예제 #1
0
        public async Task <IActionResult> GetTenants()
        {
            if (this.UserInRole(Role.Admin) || this.UserInRole(Role.Manager))
            {
                var tenants = await _tenantRepository.GetTenants();

                var tenantDTOs = new List <DTO.TenantInfoDTO>();

                foreach (var tenant in tenants)
                {
                    var unit = await _tenantRepository.UnitFromTenantId(tenant.TenantId);

                    var unitNumber = unit == null ? "" : unit.UnitNumber;
                    var tenantDTO  = new DTO.TenantInfoDTO(tenant, unitNumber);
                    tenantDTOs.Add(tenantDTO);
                }

                return(new ObjectResult(tenantDTOs));
            }
            else
            {
                var err = new DTO.ErrorBuilder()
                          .Message("You are not authorized to view tenant lists.")
                          .Code(403)
                          .Build();
                return(err);
            }
        }
        public async Task <IActionResult> GetMaintenanceRequests(int limit)
        {
            if (this.UserInRole(Role.Tenant))
            {
                var userId   = this.UserIdFromApiKey();
                var tenantId = await _tenantRepository.TenantIdFromUserId(userId);

                if (tenantId == null)
                {
                    var err = new DTO.ErrorBuilder()
                              .Message("Not a tenant.")
                              .Code(400)
                              .Build();
                    return(err);
                }
                var unit = await _tenantRepository.UnitFromTenantId((int)tenantId);

                if (unit == null)
                {
                    var err = new DTO.ErrorBuilder()
                              .Message("Tenant not assigned a unit.")
                              .Code(400)
                              .Build();
                    return(err);
                }

                var requests = await _maintenanceRepository.GetMaintenanceRequests(unit.UnitNumber);

                var requestDTOs = await MakeDTORequests(requests);

                return(new ObjectResult(requestDTOs));
            }
            else if (this.UserInRole(Role.Manager) || this.UserInRole(Role.Admin))
            {
                var requests = await _maintenanceRepository.GetMaintenanceRequests();

                var requestDTOs = await MakeDTORequests(requests);

                return(new ObjectResult(requestDTOs));
            }
            else
            {
                var err = new DTO.ErrorBuilder()
                          .Message("You are not authorized to view maintenance requests.")
                          .Code(403)
                          .Build();
                _logger.LogWarning($"Unauthorized access attempt to view maintenance requests.");
                return(err);
            }
        }