Nickolaki
Nickolaki
Explore posts from servers
CC#
Created by Nickolaki on 8/5/2023 in #help
✅ Override JWT Bearer service for Tests
Btw I managed to get it sorted with something like this:
builder.ConfigureTestServices(services =>
{
services.RemoveJwtAuthentication();

services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
{
SecurityKey issuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("9f7b309b-1dcc-4a96-a292-dbe6e830d8c3"));

options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "TestIssuer",
ValidAudience = "TestAudience",
ValidateIssuer = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ClockSkew = TimeSpan.Zero,
IssuerSigningKey = issuerSigningKey
};
}
);

services.RemoveDbContext<JobbyDbContext>();

services.AddDbContext<JobbyDbContext>(options =>
{
options.UseSqlServer(_mssqlContainer.GetConnectionString());
});
});
builder.ConfigureTestServices(services =>
{
services.RemoveJwtAuthentication();

services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
{
SecurityKey issuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("9f7b309b-1dcc-4a96-a292-dbe6e830d8c3"));

options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "TestIssuer",
ValidAudience = "TestAudience",
ValidateIssuer = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ClockSkew = TimeSpan.Zero,
IssuerSigningKey = issuerSigningKey
};
}
);

services.RemoveDbContext<JobbyDbContext>();

services.AddDbContext<JobbyDbContext>(options =>
{
options.UseSqlServer(_mssqlContainer.GetConnectionString());
});
});
12 replies
CC#
Created by Nickolaki on 8/5/2023 in #help
✅ Override JWT Bearer service for Tests
No worries bro, would really appreciate it!
12 replies
CC#
Created by Nickolaki on 8/4/2023 in #help
❔ ConfigureTestServices not overriding connection string value :(
Working! Thank you bro
public static void RemoveDbContext<T>(this IServiceCollection services) where T : DbContext
{
var dbContextOptionsService = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<T>));

if (dbContextOptionsService != null)
{
services.Remove(dbContextOptionsService);
}


var dbContextService = services.SingleOrDefault(d => d.ServiceType == typeof(T));
if (dbContextService != null)
{
services.Remove(dbContextService);
}
}
public static void RemoveDbContext<T>(this IServiceCollection services) where T : DbContext
{
var dbContextOptionsService = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<T>));

if (dbContextOptionsService != null)
{
services.Remove(dbContextOptionsService);
}


var dbContextService = services.SingleOrDefault(d => d.ServiceType == typeof(T));
if (dbContextService != null)
{
services.Remove(dbContextService);
}
}
11 replies
CC#
Created by Nickolaki on 8/4/2023 in #help
❔ ConfigureTestServices not overriding connection string value :(
Will let you know how it goes 🤣🤣
11 replies
CC#
Created by Nickolaki on 8/4/2023 in #help
❔ ConfigureTestServices not overriding connection string value :(
Cheers boss
11 replies
CC#
Created by Nickolaki on 8/4/2023 in #help
❔ ConfigureTestServices not overriding connection string value :(
DbContextOptions or something?
11 replies
CC#
Created by Nickolaki on 8/4/2023 in #help
❔ ConfigureTestServices not overriding connection string value :(
Ah damn, I had thought it was something like this. Will give it a go boss. What type is context options?
11 replies
CC#
Created by Nickolaki on 3/7/2023 in #help
❔ Querying Many to Many EF Relationships
UPDATE: I've gone with this:
public sealed class GetJobContactsSpecification : Specification<Contact>
{
public GetJobContactsSpecification(Guid jobId, string userId)
{
Query
.Include(x => x.Companies)
.Include(x => x.Phones)
.Include(x => x.Emails)
.AsSplitQuery()
.AsNoTracking()
.Where(contact=> contact.Jobs.Any(job => job.Id == jobId && job.OwnerId == userId));
}
}
public sealed class GetJobContactsSpecification : Specification<Contact>
{
public GetJobContactsSpecification(Guid jobId, string userId)
{
Query
.Include(x => x.Companies)
.Include(x => x.Phones)
.Include(x => x.Emails)
.AsSplitQuery()
.AsNoTracking()
.Where(contact=> contact.Jobs.Any(job => job.Id == jobId && job.OwnerId == userId));
}
}
However, I've previously had performance issues with using .Any() 😭
3 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
Remove the area and controller bit on the http attribute as I’m pretty sure that’s what’s causing the confusion
19 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
Then I think you need to rename the http on the a search action to this:
[HttpGet("Search/{value}")]
public async Task<IActionResult> Search(string value)
{
var response = await _mediator.Send( new SearchStudentAbsencesQuery( value ) );
var model = _mapper.Map<List<StudentAbsenceIndexVm>>( response.Entities );

ViewBag.SearchValue = value;

return View( model );
}
[HttpGet("Search/{value}")]
public async Task<IActionResult> Search(string value)
{
var response = await _mediator.Send( new SearchStudentAbsencesQuery( value ) );
var model = _mapper.Map<List<StudentAbsenceIndexVm>>( response.Entities );

ViewBag.SearchValue = value;

return View( model );
}
19 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
Have you got the area attribute named on each of the controllers?
19 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
That can be your model
19 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
public class SearchViewModel { public string Query { get; set; } public string Controller { get; set; } public string Area { get; set; } }
19 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
With the return redirect to view code
19 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
Replace your: return View(model)
19 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
Rather than the form only submit the search query. Have it post a model that contains the search query, area and controller name.
19 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
Think you can use: return RedirectToRoute(new { area= “”, controller = "", action = "", model = model });
19 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
Lemme re-write the action, on my phone so bare with
19 replies
CC#
Created by M B V R K on 8/27/2022 in #help
How to set the `Area` and `Controller` dynamically in a form of a `Partial View` ?
You could make it so that on the submit of the search form it sends a string values of the area/controller it needs to go to. These could be hidden inputs.
19 replies
CC#
Created by Nickolaki on 8/25/2022 in #help
How to use RedirectToAction(), RedirectToRoute() or Redirect() to specific endpoints on this Action?
3 replies