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
11 changes: 10 additions & 1 deletion Tracker/Controllers/CategoryController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Tracker.Entitites.Enums;
using Tracker.Entitites.Filters;
using Tracker.Interfaces.ServiceInterfaces;
Expand All @@ -26,10 +28,13 @@ public CategoryController(ICategoryService categoryService)
// _validationService = validationService;
//}

[Authorize(Roles = "Admin")]
[ServiceFilter(typeof(ValidateCategoryNameFilter))]
[HttpPost("CreateCategory")]
public async Task<IActionResult> CreateCategoryAsync(string name)
{
var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

if (!string.IsNullOrEmpty(name))
{
var createdCategory = await _categoryService.CreateCategoryAsync(name);
Expand All @@ -42,9 +47,12 @@ public async Task<IActionResult> CreateCategoryAsync(string name)
}
}

[Authorize(Policy = "AdminPolicy")]
[HttpGet("GetAllCategories")]
public async Task<IActionResult> GetAllCategoriesAsync()
{
var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

var categories = await _categoryService.GetAllCategoriesAsync();

return Ok(categories);
Expand All @@ -63,6 +71,7 @@ public async Task<IActionResult> RenameCategoryAsync(string newName, int id)
return Ok(renamedCategory);
}

[Authorize(Policy = "UserPolicy")]
[HttpDelete("DeleteCategory")]
public async Task<IActionResult> DeleteCategoryAsync(int id)
{
Expand Down
167 changes: 167 additions & 0 deletions Tracker/Migrations/20250317163008_Init.Designer.cs

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

109 changes: 109 additions & 0 deletions Tracker/Migrations/20250317163008_Init.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace Tracker.Migrations
{
/// <inheritdoc />
public partial class Init : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Categories",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
UserId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Categories", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Goals",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: false),
DeadLine = table.Column<DateTime>(type: "datetime2(0)", precision: 0, nullable: false),
DailyLimit = table.Column<TimeSpan>(type: "time", nullable: false),
IsFinished = table.Column<bool>(type: "bit", nullable: false, defaultValue: false),
CreatedAt = table.Column<DateTime>(type: "datetime2(0)", precision: 0, nullable: false),
FinishedAt = table.Column<DateTime>(type: "datetime2(0)", precision: 0, nullable: true),
CategoryId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Goals", x => x.Id);
table.ForeignKey(
name: "FK_Goals_Categories_CategoryId",
column: x => x.CategoryId,
principalTable: "Categories",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.CreateTable(
name: "Sessions",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
StartSession = table.Column<DateTime>(type: "datetime2(0)", precision: 0, nullable: false),
EndSession = table.Column<DateTime>(type: "datetime2(0)", precision: 0, nullable: true),
CategoryId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Sessions", x => x.Id);
table.ForeignKey(
name: "FK_Sessions_Categories_CategoryId",
column: x => x.CategoryId,
principalTable: "Categories",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});

migrationBuilder.InsertData(
table: "Categories",
columns: new[] { "Id", "Name", "UserId" },
values: new object[,]
{
{ 1, "Programming", 0 },
{ 2, "English", 0 }
});

migrationBuilder.CreateIndex(
name: "IX_Goals_CategoryId",
table: "Goals",
column: "CategoryId");

migrationBuilder.CreateIndex(
name: "IX_Sessions_CategoryId",
table: "Sessions",
column: "CategoryId");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Goals");

migrationBuilder.DropTable(
name: "Sessions");

migrationBuilder.DropTable(
name: "Categories");
}
}
}
Loading