Shadow Wizard Money Gang
Shadow Wizard Money Gang
CC#
Created by Shadow Wizard Money Gang on 7/13/2024 in #help
✅ CORS issue when making GET request to server running asp application.
Hi, I have this api: https://wsapi.tyrells.net/api/categories And this webapp: https://wordsearch.tyrells.net/ This is the CORS code in the api:
c#
var app = builder.Build();

app.UseCors(builder => builder.WithOrigins(["https://wordsearch.tyrells.net/", "https://wordsearch-tyrells-net.pages.dev/"])
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
c#
var app = builder.Build();

app.UseCors(builder => builder.WithOrigins(["https://wordsearch.tyrells.net/", "https://wordsearch-tyrells-net.pages.dev/"])
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
This is the error in the webapp console:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://wsapi.tyrells.net/api/categories. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 200.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://wsapi.tyrells.net/api/categories. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 200.
The server receives the request when fetching from the webapp.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (14ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "c"."name", "c"."id"
FROM "categories" AS "c"
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (14ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "c"."name", "c"."id"
FROM "categories" AS "c"
Making a GET request works fine when opening https://wsapi.tyrells.net/api/categories in browser. Have I done something blatantly incorrect? I have no idea where I've gone wrong. Thanks
75 replies
CC#
Created by Shadow Wizard Money Gang on 7/11/2024 in #help
✅Shuffling db results using LINQ.
Is there a way to shuffle db results using linq? I've tried copying some code from online using .orderby(...) but it doesnt seem to work. I have this code that returns all words from a db, shuffles them all then returns just the first x but it's horribly inefficient.
c#
[HttpGet]
public async Task<List<WordDto>> Get(string? categoryName)
{
var words = _db.Words.AsQueryable();

if (categoryName is not null)
{
words = words
.Where(w => w.Category.Name == categoryName);
}

var wordsList = await words
.Select(w => new WordDto(w.Id, w.Text, w.Length, w.Category))
.ToListAsync();

//Shuffling all words in a category. Horribly ineffcient.
wordsList.Shuffle();
//Return first x words.
return wordsList.Slice(0, 3);
}
c#
[HttpGet]
public async Task<List<WordDto>> Get(string? categoryName)
{
var words = _db.Words.AsQueryable();

if (categoryName is not null)
{
words = words
.Where(w => w.Category.Name == categoryName);
}

var wordsList = await words
.Select(w => new WordDto(w.Id, w.Text, w.Length, w.Category))
.ToListAsync();

//Shuffling all words in a category. Horribly ineffcient.
wordsList.Shuffle();
//Return first x words.
return wordsList.Slice(0, 3);
}
19 replies
CC#
Created by Shadow Wizard Money Gang on 7/9/2024 in #help
✅ Help understanding how to get foreign relation using Entity Framework core.
Hi, I want to be able to join two tables to get all words in a category using entity framework core (whatever the latest version is). I have a sqlite3 database with two tables, words and categories. This is their schema: categories: name, id. words: id, text, categoryid (foreign key), length. In plain sql joining them would be easy, just something like (or with join/innerjoin)
select * from words, categories where words.categoryid = categories.id;
select * from words, categories where words.categoryid = categories.id;
when I try
[HttpGet]
public async Task<ActionResult<IEnumerable<Category>>> Get()
{
return await _db.Categories.Include(w => w.Words).ToListAsync();
}
[HttpGet]
public async Task<ActionResult<IEnumerable<Category>>> Get()
{
return await _db.Categories.Include(w => w.Words).ToListAsync();
}
I get this error.
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Name.
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Words.Category.Name.
This is the on model creating
modelBuilder.Entity<Word>(entity =>
{
entity.HasOne(d => d.Category).WithMany(p => p.Words).OnDelete(DeleteBehavior.ClientSetNull);
});
modelBuilder.Entity<Word>(entity =>
{
entity.HasOne(d => d.Category).WithMany(p => p.Words).OnDelete(DeleteBehavior.ClientSetNull);
});
these are the class models
[Table("categories")]
public partial class Category
{
[Column("name")]
public string Name { get; set; } = null!;

[Key]
[Column("id")]
public int Id { get; set; }

[InverseProperty("Category")]
public virtual ICollection<Word> Words { get; set; } = new List<Word>();
}
[Table("categories")]
public partial class Category
{
[Column("name")]
public string Name { get; set; } = null!;

[Key]
[Column("id")]
public int Id { get; set; }

[InverseProperty("Category")]
public virtual ICollection<Word> Words { get; set; } = new List<Word>();
}
[Table("words")]
public partial class Word
{
//normal word columns

[ForeignKey("Categoryid")]
[InverseProperty("Words")]
public virtual Category Category { get; set; } = null!;
}
[Table("words")]
public partial class Word
{
//normal word columns

[ForeignKey("Categoryid")]
[InverseProperty("Words")]
public virtual Category Category { get; set; } = null!;
}
12 replies
CC#
Created by Shadow Wizard Money Gang on 6/14/2024 in #help
✅ Jittery CSS pan animation
Hi I'm having trouble trying to make a background pan animation in CSS. It pans but it's very jittery. Does anyone have an idea as to why? Code: https://hastebin.com/share/ewodonebim.javascript Demo (delete the post if not allowed to post personal links): https://www.tyrells.net/
34 replies