Timo Martinson
Timo Martinson
Explore posts from servers
DDeno
Created by Timo Martinson on 3/3/2024 in #help
How to start using data validation in kv?
Hey fellows! I want to dive into data validation. Any experience or suggestions? Feel free to post a commented version of the repository I provided here:
import Post from '@/modules/post/model.ts'

import { setData, getData, listData, removeData } from '@/helpers/data.ts'

type PostParams = {
blogSlug?: string
postSlug?: string
postInput?: Post
}

class PostRepository {
static async createPost({ postInput, blogSlug }: PostParams) {
const key = ['blog', blogSlug as string, 'post', postInput?.slug as string]
const result = await setData({ key, value: postInput })
if (result.ok) return await getData({ key })
}

static async listPosts({ blogSlug }: PostParams) {
const prefix = ['blog', blogSlug as string, 'post']
return await listData({ prefix })
}

static async showPost({ blogSlug, postSlug }: PostParams) {
const key = ['blog', blogSlug as string, 'post', postSlug as string]
return await getData({ key })
}

static async updatePost({ postInput, blogSlug, postSlug }: PostParams) {
const key = ['blog', blogSlug as string, 'post', postSlug as string]
return await setData({ key, value: postInput })
}

static async deletePost({ blogSlug, postSlug }: PostParams) {
const key = ['blog', blogSlug as string, 'post', postSlug as string]
await removeData({ key })
}
}

export default PostRepository
import Post from '@/modules/post/model.ts'

import { setData, getData, listData, removeData } from '@/helpers/data.ts'

type PostParams = {
blogSlug?: string
postSlug?: string
postInput?: Post
}

class PostRepository {
static async createPost({ postInput, blogSlug }: PostParams) {
const key = ['blog', blogSlug as string, 'post', postInput?.slug as string]
const result = await setData({ key, value: postInput })
if (result.ok) return await getData({ key })
}

static async listPosts({ blogSlug }: PostParams) {
const prefix = ['blog', blogSlug as string, 'post']
return await listData({ prefix })
}

static async showPost({ blogSlug, postSlug }: PostParams) {
const key = ['blog', blogSlug as string, 'post', postSlug as string]
return await getData({ key })
}

static async updatePost({ postInput, blogSlug, postSlug }: PostParams) {
const key = ['blog', blogSlug as string, 'post', postSlug as string]
return await setData({ key, value: postInput })
}

static async deletePost({ blogSlug, postSlug }: PostParams) {
const key = ['blog', blogSlug as string, 'post', postSlug as string]
await removeData({ key })
}
}

export default PostRepository
Thank you!
6 replies
CC#
Created by Timo Martinson on 3/3/2024 in #help
Annotate Props for Entity Framework
Which annotations do exist and how do I import them? I especially need UNIQUE contraints for the columns: [slug, id]
47 replies
CC#
Created by Timo Martinson on 3/1/2024 in #help
Breaking a cycle (Post > Blog > Post > Blog > ...)
Hey fellows! I stumbled into a cycle and don't know how to break it. It occurs between posts and blogs. This is the controller action:
[HttpPost("/blog/{blogSlug}/post")]
public async Task<Post> CreatePost([FromBody] PostInput postInput, [FromRoute] string blogSlug)
{
User user = await _dataContext.Users.FirstAsync((u) => u.Slug == "timo");
Blog blog = await _dataContext.Blogs.FirstAsync((b) => b.Slug == blogSlug);
Post newPost = PostMapper.ToPost(postInput, user: user, blog: blog);
_dataContext.Posts.Add(newPost);
await _dataContext.SaveChangesAsync();

return newPost;
}
[HttpPost("/blog/{blogSlug}/post")]
public async Task<Post> CreatePost([FromBody] PostInput postInput, [FromRoute] string blogSlug)
{
User user = await _dataContext.Users.FirstAsync((u) => u.Slug == "timo");
Blog blog = await _dataContext.Blogs.FirstAsync((b) => b.Slug == blogSlug);
Post newPost = PostMapper.ToPost(postInput, user: user, blog: blog);
_dataContext.Posts.Add(newPost);
await _dataContext.SaveChangesAsync();

return newPost;
}
How to break the cycle? Thanks.
47 replies
CC#
Created by Timo Martinson on 3/1/2024 in #help
Warning: NULL literal or a possible NULL value is being converted to a non-nullable type.
Ladies and gentlemen, I am confronted with a new type of warning, that I would like to understand. What should I do? This is the code:
[HttpGet("/{userSlug}")]
public async Task<User> ShowUser(string userSlug)
{
User user = await _dataContext.Users.FirstOrDefaultAsync((u) => u.Slug == userSlug);

return user;
}
[HttpGet("/{userSlug}")]
public async Task<User> ShowUser(string userSlug)
{
User user = await _dataContext.Users.FirstOrDefaultAsync((u) => u.Slug == userSlug);

return user;
}
This is the message: NULL literal or a possible NULL value is being converted to a non-nullable type
8 replies
CC#
Created by Timo Martinson on 3/1/2024 in #help
Preparing Models for an API (How to setup relationships)
Hey fellows! I wonder how to set the properties for related models ... Here is my model:
public class Comment(string name, string content)
{
public Guid Id { get; set; } = Guid.NewGuid();

public string Name { get; set; } = name;
public string Content { get; set; } = content;

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;

public User? User { get; set; }
public Post? Post { get; set; }
}
public class Comment(string name, string content)
{
public Guid Id { get; set; } = Guid.NewGuid();

public string Name { get; set; } = name;
public string Content { get; set; } = content;

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;

public User? User { get; set; }
public Post? Post { get; set; }
}
Thank you for your help.
190 replies
TSDThe Swift Den
Created by Timo Martinson on 1/5/2024 in #swift-development
how to randomly display data in SwiftUI?
typical use case for flash cards... I have shelves that contain boxes that contain cards. i want to randomly select a card from a box and display its data (front) and accept user input (right or wrong) and increment card.level if user selects "right" or decrement if he/she selects "wrong", after he/she has seen the back of the card. How to approach this use case? Have a nice day, and thank you!
4 replies
CC#
Created by Timo Martinson on 11/24/2023 in #help
how to split code to multiple files in minimal api?
How to do that?
5 replies
DTDrizzle Team
Created by Timo Martinson on 11/24/2023 in #help
Where to get the types from?
I want to use the types of my schema parts. How can I include them? Thanks!!!
2 replies
CC#
Created by Timo Martinson on 9/27/2023 in #help
❔ Make Entity Framework Property Unique
namespace Textopia.Models;

public class Blog(string slug, string? name = null, string? description = null)
{
public Guid Id { get; set; } = Guid.NewGuid();

public string Slug { get; set; } = slug;
public string? Name { get; set; } = name;
public string? Description { get; set; } = description;

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
public DateTime? DeletedAt { get; set; }

public ICollection<User>? Users { get; set; }
public ICollection<Post>? Posts { get; set; }
}
namespace Textopia.Models;

public class Blog(string slug, string? name = null, string? description = null)
{
public Guid Id { get; set; } = Guid.NewGuid();

public string Slug { get; set; } = slug;
public string? Name { get; set; } = name;
public string? Description { get; set; } = description;

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
public DateTime? DeletedAt { get; set; }

public ICollection<User>? Users { get; set; }
public ICollection<Post>? Posts { get; set; }
}
How can I make slugs unique!?
12 replies
CC#
Created by Timo Martinson on 9/27/2023 in #help
❔ Initializing Entity Relationship Properties
namespace Textopia.Models;

public class Post(string slug)
{
public Guid Id { get; set; } = Guid.NewGuid();

public string Slug { get; set; } = slug;
public string? Name { get; set; }
public string? Content { get; set; }

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
public DateTime? DeletedAt { get; set; }

public User User { get; set; }
public Blog Blog { get; set; }
}
namespace Textopia.Models;

public class Post(string slug)
{
public Guid Id { get; set; } = Guid.NewGuid();

public string Slug { get; set; } = slug;
public string? Name { get; set; }
public string? Content { get; set; }

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
public DateTime? DeletedAt { get; set; }

public User User { get; set; }
public Blog Blog { get; set; }
}
How to initialize User and Blog property? Thaaaaaaaanks. 😉
17 replies
CC#
Created by Timo Martinson on 8/31/2023 in #help
❔ Setting up a One-To-Many-Relationship in EntityFramework Core
namespace GetOut.Models;

public class Provider(string slug, string name, string? description)
{
public Guid Id { get; set; } = Guid.NewGuid();

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
public DateTime? DeletedAt { get; set; }

public string Slug { get; set; } = slug;
public string Name { get; set; } = name;
public string? Description { get; set; } = description;

public bool Verified { get; set; } = false;

public City City { get; set; }
public ICollection<Event>? Events { get; set; }
}
namespace GetOut.Models;

public class Provider(string slug, string name, string? description)
{
public Guid Id { get; set; } = Guid.NewGuid();

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
public DateTime? DeletedAt { get; set; }

public string Slug { get; set; } = slug;
public string Name { get; set; } = name;
public string? Description { get; set; } = description;

public bool Verified { get; set; } = false;

public City City { get; set; }
public ICollection<Event>? Events { get; set; }
}
I want to establish a relation between Providers and City. 😉 Thanks.
15 replies
CC#
Created by Timo Martinson on 8/30/2023 in #help
❔ How to bring in DbContext?
app.MapPost("/cities", async ([FromBody] City city) => {
...
});
app.MapPost("/cities", async ([FromBody] City city) => {
...
});
21 replies
CC#
Created by Timo Martinson on 8/29/2023 in #help
❔ how to get city data from request body?
app.MapPost("/cities", async (DataContext dataContext) => {
dataContext.Cities.Add(city);
await dataContext.SaveChangesAsync();
return Results.Created();
});
app.MapPost("/cities", async (DataContext dataContext) => {
dataContext.Cities.Add(city);
await dataContext.SaveChangesAsync();
return Results.Created();
});
10 replies
DDeno
Created by Timo Martinson on 8/21/2023 in #help
Property 'params' does not exist on type 'Context<State, Record<string, any>> ... what to do?
I want to access params in oak, but typescript complains. What can I do about that? Thanks.
3 replies
CC#
Created by Timo Martinson on 8/4/2023 in #help
❔ Initializing a relation in Entity Framework Core?
public class Post : Model
{
public string Slug { get; set; }
public string? Name { get; set; }
public string? Content { get; set; }
public bool Draft { get; set; } = true;
public DateTime PublishedAt { get; set; } = DateTime.Now;

public User User { get; set; }
public Blog Blog { get; set; }
public ICollection<Comment>? Comments { get; set; }
public ICollection<Label>? Labels { get; set; }

public Post(string slug, string? name, string? content) : base()
{
Slug = slug;
Name = name;
Content = content;
}
}
public class Post : Model
{
public string Slug { get; set; }
public string? Name { get; set; }
public string? Content { get; set; }
public bool Draft { get; set; } = true;
public DateTime PublishedAt { get; set; } = DateTime.Now;

public User User { get; set; }
public Blog Blog { get; set; }
public ICollection<Comment>? Comments { get; set; }
public ICollection<Label>? Labels { get; set; }

public Post(string slug, string? name, string? content) : base()
{
Slug = slug;
Name = name;
Content = content;
}
}
How can I initialize User and Blog with EntityFrameworkCore?
17 replies
DDeno
Created by Timo Martinson on 8/1/2023 in #help
How to access request body in Deno?
Thank you.
2 replies
DDeno
Created by Timo Martinson on 7/22/2023 in #help
Where to store this key — and how?
const cryptoKey =
await crypto.subtle.generateKey({
name: 'HMAC', hash: 'SHA-512'
}, true, ['sign', 'verify'])
const cryptoKey =
await crypto.subtle.generateKey({
name: 'HMAC', hash: 'SHA-512'
}, true, ['sign', 'verify'])
5 replies
DDeno
Created by Timo Martinson on 7/22/2023 in #help
Typing DateTimes
interface ICity {
slug: string
name: string
description?: string
createdAt: Date
updatedAt: Date
}
interface ICity {
slug: string
name: string
description?: string
createdAt: Date
updatedAt: Date
}
can I save strings like "2023-09-10 18:16:00" as createdAt and updatedAt ?
7 replies
DDeno
Created by Timo Martinson on 7/20/2023 in #help
KV: What to return from here?
// Create City
router.post('/cities', async (context) => {
const data = await context.request.body().value
if (data.slug && data.name) {
await kv.set(['cities', data.slug], {
slug: data.slug,
name: data.name,
description: data.description
} satisfies ICity)
}
})
// Create City
router.post('/cities', async (context) => {
const data = await context.request.body().value
if (data.slug && data.name) {
await kv.set(['cities', data.slug], {
slug: data.slug,
name: data.name,
description: data.description
} satisfies ICity)
}
})
24 replies
DDeno
Created by Timo Martinson on 7/4/2023 in #help
Absolute Imports with Oak
I would like to have absolute imports in my Oak-API ... 😉
5 replies