public void Setup() { _mockContext = SetupMockDataContext(); var claim = new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", _mockContext.Object.Users.Single(x => x.UserName == "adhach").Id.ToString()); _adHachClaims = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { claim })); claim = new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", _mockContext.Object.Users.Single(x => x.UserName == "tnt01user").Id.ToString()); _tnt01Claims = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { claim })); claim = new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", _mockContext.Object.Users.Single(x => x.UserName == "tnt01and02user").Id.ToString()); _tnt01And02Claims = new ClaimsPrincipal(new ClaimsIdentity(new List <Claim> { claim })); Thread.CurrentPrincipal = _tnt01Claims; var validator = new DashboardOptionValidator(); _facade = new DashboardOptionFacade(_mockContext.Object, validator); _toCreateHachFusion = new DashboardOption() { Id = Guid.Parse("7DDD52F4-A21D-4B99-A87F-A837DA940D40"), TenantId = _mockContext.Object.Tenants.Single(x => x.Name == "Hach Fusion").Id, Options = "HachFusion_Options" }; }
/// <summary> /// Creates a dashboard option. /// </summary> /// <param name="dto">Data Transfer Object (DTO) used to create an entity.</param> /// <returns> /// An asynchronous task result containing information needed to create an API response message. /// If successful, the task result contains the DTO associated with the entity created. /// </returns> public override async Task <CommandResult <DashboardOptionQueryDto, Guid> > Create(DashboardOptionBaseDto dto) { var userId = Thread.CurrentPrincipal == null ? null : Thread.CurrentPrincipal.GetUserIdFromPrincipal(); if (userId == null) { return(Command.Error <DashboardOptionQueryDto>(GeneralErrorCodes.TokenInvalid("UserId"))); } var validationResponse = ValidatorCreate.Validate(dto); if (dto == null) { return(Command.Error <DashboardOptionQueryDto>(validationResponse)); } if (dto.Id != Guid.Empty) { validationResponse.FFErrors.Add(ValidationErrorCode.PropertyIsInvalid("Id")); } var userTenants = _context.GetTenantsForUser(Guid.Parse(userId)); if (dto.TenantId != Guid.Empty && !userTenants.Any(x => x.Id == dto.TenantId)) { validationResponse.FFErrors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist("TenantId")); } else if (_context.DashboardOptions.Any(x => x.TenantId == dto.TenantId)) { validationResponse.FFErrors.Add(EntityErrorCode.EntityAlreadyExists); } if (validationResponse.IsInvalid) { return(Command.Error <DashboardOptionQueryDto>(validationResponse)); } var newEntity = new DashboardOption(); _mapper.Map(dto, newEntity); newEntity.Id = Guid.NewGuid(); newEntity.SetAuditFieldsOnCreate(userId); _context.DashboardOptions.Add(newEntity); await _context.SaveChangesAsync().ConfigureAwait(false); return(Command.Created(_mapper.Map(newEntity, new DashboardOptionQueryDto()), newEntity.Id)); }