Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ riderModule.iml
/_ReSharper.Caches/
.fake
.vscode
.vs/
Binary file removed .vs/MyFirstBlog/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
1,016 changes: 0 additions & 1,016 deletions .vs/MyFirstBlog/config/applicationhost.config

This file was deleted.

Binary file removed .vs/MyFirstBlog/v17/.futdcache.v2
Binary file not shown.
Binary file removed .vs/MyFirstBlog/v17/.suo
Binary file not shown.
149 changes: 0 additions & 149 deletions .vs/MyFirstBlog/v17/DocumentLayout.json

This file was deleted.

Binary file removed .vs/ProjectEvaluation/myfirstblog.metadata.v7.bin
Binary file not shown.
Binary file removed .vs/ProjectEvaluation/myfirstblog.projects.v7.bin
Binary file not shown.
74 changes: 53 additions & 21 deletions MyFirstBlog/Controllers/PostsController.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,66 @@
namespace MyFirstBlog.Controllers;

using Microsoft.AspNetCore.Mvc;
using MyFirstBlog.Dtos;
using MyFirstBlog.Services;
using System.Collections.Generic;

[ApiController]
[Route("posts")]
namespace MyFirstBlog.Controllers
{
[ApiController]
[Route("posts")]
public class PostsController : ControllerBase
{
private readonly IPostService _postService;

public class PostsController : ControllerBase {
private IPostService _postService;
public PostsController(IPostService postService)
{
_postService = postService;
}

public PostsController(IPostService postService) {
_postService = postService;
}
// GET /posts
[HttpGet]
public IEnumerable<PostDto> GetPosts()
{
return _postService.GetPosts();
}

// Get /posts
[HttpGet]
public IEnumerable<PostDto> GetPosts() {
return _postService.GetPosts();
}
// GET /posts/{slug}

[HttpGet("{slug}")]
public ActionResult<PostDto> GetPost(string slug)
{
var post = _postService.GetPost(slug);
if (post == null)
{
return NotFound();
}
return Ok(post);
}



[HttpPost]
public IActionResult CreatePost([FromBody] PostCreateRequest post)
{
var errors = new List<string>();

if (string.IsNullOrWhiteSpace(post.Title))
errors.Add("Title cannot be blank");

if (string.IsNullOrWhiteSpace(post.Description))
errors.Add("You cannot send a message with empty description");

if (errors.Any())
return BadRequest(new { errors });

// Get /posts/:slug
[HttpGet("{slug}")]
public ActionResult<PostDto> GetPost(string slug) {
var post = _postService.GetPost(slug);
var newPost = _postService.CreatePost(post);

if (post is null) {
return NotFound();
return CreatedAtAction(nameof(GetPost), new { slug = newPost.Slug }, new
{
title = newPost.Title,
description = newPost.Description
});
//return CreatedAtAction(nameof(GetPost), new { slug = newPost.Slug }, newPost);
}

return post;
}
}
8 changes: 8 additions & 0 deletions MyFirstBlog/Dtos/PostCreateRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MyFirstBlog.Dtos
{
public class PostCreateRequest
{
public string Title { get; set; }
public string Description { get; set; }
}
}
1 change: 1 addition & 0 deletions MyFirstBlog/Dtos/PostDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public record PostDto {
public string Title { get; init; } = default!;
public string Slug { get; init; } = default!;
public string Body { get; init; } = default!;
public string Description { get; set; } = default!;
public DateTime CreatedDate { get; init; }
}
1 change: 1 addition & 0 deletions MyFirstBlog/Entities/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public record Post {
public string Title { get; init; } = default!;
public string Slug { get; init; } = default!;
public string Body { get; init; } = default!;
public string Description { get; set; }
public DateTime CreatedDate { get; init; }
}
1 change: 1 addition & 0 deletions MyFirstBlog/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static PostDto AsDto(this Post post) {
Title = post.Title,
Slug = post.Slug,
Body = post.Body,
Description = post.Description,
CreatedDate = post.CreatedDate
};

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions MyFirstBlog/Migrations/20250807001933_AddDescriptionToPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MyFirstBlog.Migrations
{
/// <inheritdoc />
public partial class AddDescriptionToPost : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Description",
table: "Posts",
type: "text",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Description",
table: "Posts");
}
}
}
Loading