wwww
wwww
CC#
Created by wwww on 10/13/2023 in #help
✅ Unit testing
Hi chat, should i unit test actions like that? i mean... like... is it any point in that?
[HttpGet]
public IActionResult GetAllChats()
{
if (_db.Chats.Any())
{
return Json(_db.Chats.ToList());
}
else
{
return NotFound("There is no chats at this moment");
}
}
[HttpGet]
public IActionResult GetAllChats()
{
if (_db.Chats.Any())
{
return Json(_db.Chats.ToList());
}
else
{
return NotFound("There is no chats at this moment");
}
}
[HttpPost]
public async Task<IActionResult> CreateChat([FromBody] CreateChatDto chat)
{
_db.Chats.Add(new Chat
{
AuthorId = (int)chat.AuthorId!,
Title = chat.Title
});
await _db.SaveChangesAsync();

return Ok("Chat successfully created");
}
[HttpPost]
public async Task<IActionResult> CreateChat([FromBody] CreateChatDto chat)
{
_db.Chats.Add(new Chat
{
AuthorId = (int)chat.AuthorId!,
Title = chat.Title
});
await _db.SaveChangesAsync();

return Ok("Chat successfully created");
}
i mean, i could test that. but don't see any point do test previous 2
[HttpDelete]
public async Task<IActionResult> DeleteChat([FromBody] DeleteChatDTO helper)
{
Chat? chat = await _db.Chats.FirstOrDefaultAsync(chat => chat.Id == helper.ChatId);

if (chat == null)
return NotFound("Chat with this Id not found");

if (chat.AuthorId != helper.SenderId)
return BadRequest("this user doesn't have permissions");

await DisconnectAllUsersFromChatAsync(helper.ChatId);

_db.Chats.Remove(chat);
await _db.SaveChangesAsync();

return Ok("Chat successfully deleted");
}
[HttpDelete]
public async Task<IActionResult> DeleteChat([FromBody] DeleteChatDTO helper)
{
Chat? chat = await _db.Chats.FirstOrDefaultAsync(chat => chat.Id == helper.ChatId);

if (chat == null)
return NotFound("Chat with this Id not found");

if (chat.AuthorId != helper.SenderId)
return BadRequest("this user doesn't have permissions");

await DisconnectAllUsersFromChatAsync(helper.ChatId);

_db.Chats.Remove(chat);
await _db.SaveChangesAsync();

return Ok("Chat successfully deleted");
}
2 replies
CC#
Created by wwww on 10/12/2023 in #help
✅ EF Core delete error
hi, i tried to fix it but nothing works, so i have dbcontext
public interface IApplicationDbContext
{
DbSet<Chat> Chats { get; set; }
DbSet<Message> Messages { get; set; }
DbSet<Connection> LiveConnections { get; set; }
int SaveChanges();
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
public interface IApplicationDbContext
{
DbSet<Chat> Chats { get; set; }
DbSet<Message> Messages { get; set; }
DbSet<Connection> LiveConnections { get; set; }
int SaveChanges();
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
public class ApplicationDbContext : DbContext, IApplicationDbContext
{
public DbSet<Chat> Chats { get; set; } = null!;
public DbSet<Message> Messages { get; set; } = null!;
public DbSet<Connection> LiveConnections { get; set; } = null!;

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
Database.EnsureCreated();
}

public override int SaveChanges()
{
return base.SaveChanges();
}

public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
return await base.SaveChangesAsync(cancellationToken);
}
}
public class ApplicationDbContext : DbContext, IApplicationDbContext
{
public DbSet<Chat> Chats { get; set; } = null!;
public DbSet<Message> Messages { get; set; } = null!;
public DbSet<Connection> LiveConnections { get; set; } = null!;

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
Database.EnsureCreated();
}

public override int SaveChanges()
{
return base.SaveChanges();
}

public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
return await base.SaveChangesAsync(cancellationToken);
}
}
and
public class Chat
{
[Key]
public int Id { get; set; }
public int? AuthorId { get; set; } = null!;
public string Title { get; set; } = null!;
public List<Connection>? LiveConnections { get; set; }
public List<Message>? Messages { get; set; }
}
public class Chat
{
[Key]
public int Id { get; set; }
public int? AuthorId { get; set; } = null!;
public string Title { get; set; } = null!;
public List<Connection>? LiveConnections { get; set; }
public List<Message>? Messages { get; set; }
}
public class Connection
{
[Key]
public int? Id { get; set; }
public string ConnectionId { get; set; } = "Undefined";
public int? ChatId { get; set; }
public int? UserId { get; set; }
}
public class Connection
{
[Key]
public int? Id { get; set; }
public string ConnectionId { get; set; } = "Undefined";
public int? ChatId { get; set; }
public int? UserId { get; set; }
}
public class Message
{
[Key]
public int? Id { get; set; }
public int? ChatId { get; set; } = null!;
public int? SenderId { get; set; } = null!;
public DateTime? Date { get; set; } = DateTime.Now;
public string? Content { get; set; }
}
public class Message
{
[Key]
public int? Id { get; set; }
public int? ChatId { get; set; } = null!;
public int? SenderId { get; set; } = null!;
public DateTime? Date { get; set; } = DateTime.Now;
public string? Content { get; set; }
}
so, error only happens when chat have Include (connections / messages). how to make on delete chat don't care about this things?
15 replies
CC#
Created by wwww on 10/12/2023 in #help
✅ Postman / SignalR
No description
8 replies
CC#
Created by wwww on 10/11/2023 in #help
✅ SignalR / Websockets
hi, i need to create app where u have x amount of chats, when u connect to chat, u can write messages and other ppl form same chat need to see it live, and all this without using any front-end, can anyone set me on the right path with some advices?
9 replies
CC#
Created by wwww on 10/10/2023 in #help
✅ GitHub/GitLab/another
No description
14 replies
CC#
Created by wwww on 10/3/2023 in #help
✅ Forms, Js and MVC
No description
8 replies
CC#
Created by wwww on 9/28/2023 in #help
✅ DB context injection not working
No description
29 replies
CC#
Created by wwww on 9/26/2023 in #help
✅ Any Frontenders? pagination
No description
23 replies
CC#
Created by wwww on 9/25/2023 in #help
✅ Server side or Client side?
Hi, finally i get to MVC and it looks much easier then work with minmal api, so i have some questions about it. should i use views for 100% of my htmlpages? like, as i get, cuz mvc is just part of net core, instead of return View() i can use app.UseDefaultFiles(); and i thing it gonna work without any problem, is it bad tone to mix ways of rendering? also, in old project i was requesting data with fetch and rendering it like this -
async function LoadProduct() {
const response = await fetch(`/api/product`, {
method: "GET",
headers: { "Accept": "application/json" }
});

if (!response.ok)
console.error();

const data = await response.json();
const div = `
<div class="product-container">
<div class="product-image">
<img class="product-img" src="../${data.image}" alt="Product Image">
</div>
<div class="product-details">
<p class="titleq pp">${data.title}</p>
<p class="priceq pp">${data.price}$</p>
<p class="descriptionq pp">${data.description}</p>
</div>
</div>
`

document.querySelector("[data-container]").insertAdjacentHTML("beforeend", div);
};
async function LoadProduct() {
const response = await fetch(`/api/product`, {
method: "GET",
headers: { "Accept": "application/json" }
});

if (!response.ok)
console.error();

const data = await response.json();
const div = `
<div class="product-container">
<div class="product-image">
<img class="product-img" src="../${data.image}" alt="Product Image">
</div>
<div class="product-details">
<p class="titleq pp">${data.title}</p>
<p class="priceq pp">${data.price}$</p>
<p class="descriptionq pp">${data.description}</p>
</div>
</div>
`

document.querySelector("[data-container]").insertAdjacentHTML("beforeend", div);
};
4 replies
CC#
Created by wwww on 9/23/2023 in #help
❔ Web things :)
No description
5 replies
CC#
Created by wwww on 9/22/2023 in #help
✅ Controller routing in 2023
Hi, as I understand it, just like everything in programming, you can do one thing in many different ways. This also works to controller routing. There are lot of ways to route your controllers in a web app. What is the most common way to route them in 2023? Setting each method from Program.cs? route attribute? maybe just with method names?
32 replies
CC#
Created by wwww on 9/22/2023 in #help
✅ Blazor or Razor pages
Hi chat, in my roadmap, i have razor pages after Asp.net core mvc, but i see lot of ppl talks about blazor and never seen someone was asking question about razor pages, what is blazor? (very shortyly) and is it better to replace razor pages with blazor, or jsut spend more time and learn both of them?
9 replies
CC#
Created by wwww on 9/13/2023 in #help
✅ Html/js Redirection also some Minimal api things
4 replies
CC#
Created by wwww on 9/12/2023 in #help
✅ Web things minimal api
69 replies
CC#
Created by wwww on 9/11/2023 in #help
✅ Fetch
hi 🙂 so, i have small problem with sending form with image to server, can someone please explain how to correctly do that? spend like 3 hours and not found anything useful about that 😦
<form id="formq">
<p>
<label for="title" method="post">Title</label>
</p>
<input type="text" id="title" name="title" required>

<p>
<label for="description">description</label>
</p>
<input type="description" id="description" name="description" required>

<p>
<label for="price">price</label>
</p>
<input type="price" id="price" name="price" required>

<label for="image">price</label>
<input type="file" id="image" name="image" required>
<p>
<button type="submit">Add product</button>
</p>
</form>
<form id="formq">
<p>
<label for="title" method="post">Title</label>
</p>
<input type="text" id="title" name="title" required>

<p>
<label for="description">description</label>
</p>
<input type="description" id="description" name="description" required>

<p>
<label for="price">price</label>
</p>
<input type="price" id="price" name="price" required>

<label for="image">price</label>
<input type="file" id="image" name="image" required>
<p>
<button type="submit">Add product</button>
</p>
</form>
that is endpoint for request, but have no clue how to send image with,
app.MapPost("/api/addProduct", [Authorize] (ProductM prod, ShopDbContext db) =>
{

string fileName = Guid.NewGuid().ToString();
// save file in folder with new name
// create Product based on data
// save to db
return Results.Ok();
});
app.MapPost("/api/addProduct", [Authorize] (ProductM prod, ShopDbContext db) =>
{

string fileName = Guid.NewGuid().ToString();
// save file in folder with new name
// create Product based on data
// save to db
return Results.Ok();
});
like, in case of registration i was sending it like -
const response = await fetch("api/register", {
method: "POST",
headers: { "Accept": "application/json", "Content-Type": "application/json" },
body: JSON.stringify({
name: document.getElementById("name").value,
email: document.getElementById("email").value,
password: document.getElementById("password").value
})
});
const response = await fetch("api/register", {
method: "POST",
headers: { "Accept": "application/json", "Content-Type": "application/json" },
body: JSON.stringify({
name: document.getElementById("name").value,
email: document.getElementById("email").value,
password: document.getElementById("password").value
})
});
but with images its a bit harder, tried with byte array but don't worked
70 replies
CC#
Created by wwww on 9/10/2023 in #help
✅ SQL and images
95 replies
CC#
Created by wwww on 9/9/2023 in #help
✅ Dark side
Hi, sooooo. Im building some web APIs, but when it comes to rendering data in html from js, its a bit annoying, like creating and appending all of these things, etc. So, I need some HTML builder, but very simple. Ive heard something about Mustache.js, or maybe there is something more relevant in 2023? And definitely not something like react.
9 replies
CC#
Created by wwww on 9/8/2023 in #help
✅ First task :)
21 replies
CC#
Created by wwww on 9/8/2023 in #help
✅ Some web things
Soooo... i have this app
using System;
using webtest;

class Prgram
{
static void Main(string[] args)
{
var dataSource = new List<Person>() {
new Person("David", 21),
new Person("Bob", 25),
new Person("Tom", 30),
new Person("Sam", 23),
};

var builder = WebApplication.CreateBuilder(
new WebApplicationOptions
{
WebRootPath = "StaticFiles"
}
);
var app = builder.Build();

app.MapGet("/users", async (context) =>
{
context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.SendFileAsync("Html/users.html");
});

app.UseWhen(
context => context.Request.Path == "/users" && context.Request.Method == "GET",
appBuilder => appBuilder.Run(async context =>
{
await context.Response.WriteAsJsonAsync(dataSource);
})
);

app.MapGet("/", async (context) =>
{
context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.SendFileAsync("Html/index.html");
});

app.Run();
}
}
using System;
using webtest;

class Prgram
{
static void Main(string[] args)
{
var dataSource = new List<Person>() {
new Person("David", 21),
new Person("Bob", 25),
new Person("Tom", 30),
new Person("Sam", 23),
};

var builder = WebApplication.CreateBuilder(
new WebApplicationOptions
{
WebRootPath = "StaticFiles"
}
);
var app = builder.Build();

app.MapGet("/users", async (context) =>
{
context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.SendFileAsync("Html/users.html");
});

app.UseWhen(
context => context.Request.Path == "/users" && context.Request.Method == "GET",
appBuilder => appBuilder.Run(async context =>
{
await context.Response.WriteAsJsonAsync(dataSource);
})
);

app.MapGet("/", async (context) =>
{
context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.SendFileAsync("Html/index.html");
});

app.Run();
}
}
problem - On home page i have link which goes to /users, i wanted it to work like, first time when go to users, send users html page, and on this users html page wanted to make <ul> with event listener "onload" to request data. any advice?
49 replies
CC#
Created by wwww on 9/7/2023 in #help
✅ REST Api
Hi, so I just want to learn what a REST API and JSON API are. When I Google it, I come across some weird websites. Of course, they might have everything about those things, but anyway, can someone concisely explain what these are and provide some sources to dive deeper into that. Maybe I already understand what this is, but I'm not very familiar with the terminology in this case. is it about HttpContext and it's methods, middlewares, routing, services etc?
17 replies