Timo Martinson
Timo Martinson
Explore posts from servers
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
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
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
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