Skip to content

Commit f5f23d2

Browse files
committed
Initial commit
0 parents  commit f5f23d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1652
-0
lines changed

.gitignore

+454
Large diffs are not rendered by default.

Controllers/TopicsController.cs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using QuizApi.Dtos;
3+
using QuizApi.Dtos.Topic;
4+
using QuizApi.Services;
5+
6+
namespace QuizApi.Controllers;
7+
8+
9+
[ApiController]
10+
[Route("api/[controller]")]
11+
public class TopicsController : ControllerBase
12+
{
13+
private readonly ILogger<TopicsController> _logger;
14+
private readonly ITopicService _service;
15+
16+
public TopicsController(
17+
ILogger<TopicsController> logger,
18+
ITopicService service)
19+
{
20+
_logger = logger;
21+
_service = service;
22+
}
23+
24+
[HttpGet]
25+
[Produces("application/json")]
26+
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ResponseBase<List<Topic>>))]
27+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
28+
public IActionResult GetTopics([FromQuery]Pagination pagination)
29+
{
30+
var dtoModels = _service.GetAll()
31+
.Skip((pagination.Page - 1) * pagination.Limit)
32+
.Take(pagination.Limit)
33+
.Select(Mappers.ModelToDto)
34+
.ToList();
35+
36+
var response = new ResponseBase<List<Topic>>()
37+
{
38+
Data = dtoModels,
39+
Pagination = pagination
40+
};
41+
42+
return Ok(response);
43+
}
44+
45+
[HttpPost]
46+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
47+
public async Task<IActionResult> PostTopic(CreateTopicDto model)
48+
{
49+
if(!ModelState.IsValid) return NotFound();
50+
51+
var ModelgaAylanganDto = Mappers.DtoToModel(model);
52+
53+
var result = await _service.CreateAsync(ModelgaAylanganDto);
54+
// Console.WriteLine($"Bu yerda malumot qoshilishi mumkin edi : {ModelgaAylanganDto}");
55+
56+
57+
if(!result.IsSuccess)
58+
{
59+
_logger.LogInformation($"Error boldi sababi: {result.exception?.Message}");
60+
return BadRequest();
61+
}
62+
// var entity = ToEntity(model);
63+
64+
// var result = await _unitOfWork.Topics.Add(entity);
65+
// _unitOfWork.Save();
66+
67+
68+
return Created("/", model);
69+
}
70+
}

Data/AppDbContext.cs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Reflection;
2+
using Microsoft.EntityFrameworkCore;
3+
using QuizApi.Entities;
4+
5+
namespace QuizApi.Data;
6+
7+
public class AppDbContext : DbContext
8+
{
9+
public DbSet<Topic>? Topics { get; set; }
10+
11+
public AppDbContext(DbContextOptions options) : base(options){}
12+
13+
// protected override void OnModelCreating(ModelBuilder modelBuilder)
14+
// {
15+
// base.OnModelCreating(modelBuilder);
16+
17+
// modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
18+
// }
19+
20+
// public override int SaveChanges()
21+
// {
22+
// // AddNameHash();
23+
// // SetDates();
24+
25+
// return base.SaveChanges();
26+
// }
27+
28+
// public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
29+
// {
30+
// // AddNameHash();
31+
// // SetDates();
32+
// return base.SaveChangesAsync(cancellationToken);
33+
// }
34+
35+
// private void SetDates()
36+
// {
37+
// foreach(var entry in ChangeTracker.Entries<EntityBase>())
38+
// {
39+
// if(entry.State == EntityState.Added)
40+
// {
41+
// entry.Entity.CreatedAt = DateTimeOffset.UtcNow;
42+
// entry.Entity.UpdatedAt = DateTimeOffset.UtcNow;
43+
// }
44+
45+
// if(entry.State == EntityState.Modified)
46+
// {
47+
// entry.Entity.UpdatedAt = DateTimeOffset.UtcNow;
48+
// }
49+
// }
50+
// }
51+
52+
// private void AddNameHash()
53+
// {
54+
// foreach(var entry in ChangeTracker.Entries<Topic>())
55+
// {
56+
// if(entry.Entity is Topic topic)
57+
// {
58+
// using var sha256 = SHA256.Create();
59+
// var nameBytes = Encoding.UTF8.GetBytes(topic.Name
60+
// ?? throw new ArgumentNullException(nameof(topic.Name)));
61+
// var hashBytes = sha256.ComputeHash(nameBytes);
62+
63+
// topic.NameHash = Encoding.UTF8.GetString(hashBytes);
64+
// }
65+
// }
66+
// }
67+
}

Data/Migrations/20220806133921_Initial_Create.Designer.cs

+53
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
4+
#nullable disable
5+
6+
namespace QuizApi.Data.Migrations
7+
{
8+
public partial class Initial_Create : Migration
9+
{
10+
protected override void Up(MigrationBuilder migrationBuilder)
11+
{
12+
migrationBuilder.CreateTable(
13+
name: "Topics",
14+
columns: table => new
15+
{
16+
Id = table.Column<ulong>(type: "INTEGER", nullable: false)
17+
.Annotation("Sqlite:Autoincrement", true),
18+
Name = table.Column<string>(type: "TEXT", nullable: true),
19+
NameHash = table.Column<string>(type: "TEXT", nullable: true),
20+
Description = table.Column<string>(type: "TEXT", nullable: true),
21+
Difficulty = table.Column<int>(type: "INTEGER", nullable: false),
22+
CreatedAt = table.Column<DateTimeOffset>(type: "TEXT", nullable: false),
23+
UpdatedAt = table.Column<DateTimeOffset>(type: "TEXT", nullable: false)
24+
},
25+
constraints: table =>
26+
{
27+
table.PrimaryKey("PK_Topics", x => x.Id);
28+
});
29+
}
30+
31+
protected override void Down(MigrationBuilder migrationBuilder)
32+
{
33+
migrationBuilder.DropTable(
34+
name: "Topics");
35+
}
36+
}
37+
}

Data/Migrations/20220807120009_Some_Changes.Designer.cs

+61
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace QuizApi.Data.Migrations
6+
{
7+
public partial class Some_Changes : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.AlterColumn<string>(
12+
name: "NameHash",
13+
table: "Topics",
14+
type: "nvarchar(64)",
15+
nullable: false,
16+
defaultValue: "",
17+
oldClrType: typeof(string),
18+
oldType: "TEXT",
19+
oldNullable: true);
20+
21+
migrationBuilder.AlterColumn<string>(
22+
name: "Name",
23+
table: "Topics",
24+
type: "varchar(50)",
25+
nullable: false,
26+
defaultValue: "",
27+
oldClrType: typeof(string),
28+
oldType: "TEXT",
29+
oldNullable: true);
30+
31+
migrationBuilder.AlterColumn<string>(
32+
name: "Description",
33+
table: "Topics",
34+
type: "nvarchar(255)",
35+
nullable: false,
36+
defaultValue: "",
37+
oldClrType: typeof(string),
38+
oldType: "TEXT",
39+
oldNullable: true);
40+
41+
migrationBuilder.CreateIndex(
42+
name: "IX_Topics_NameHash",
43+
table: "Topics",
44+
column: "NameHash",
45+
unique: true);
46+
}
47+
48+
protected override void Down(MigrationBuilder migrationBuilder)
49+
{
50+
migrationBuilder.DropIndex(
51+
name: "IX_Topics_NameHash",
52+
table: "Topics");
53+
54+
migrationBuilder.AlterColumn<string>(
55+
name: "NameHash",
56+
table: "Topics",
57+
type: "TEXT",
58+
nullable: true,
59+
oldClrType: typeof(string),
60+
oldType: "nvarchar(64)");
61+
62+
migrationBuilder.AlterColumn<string>(
63+
name: "Name",
64+
table: "Topics",
65+
type: "TEXT",
66+
nullable: true,
67+
oldClrType: typeof(string),
68+
oldType: "varchar(50)");
69+
70+
migrationBuilder.AlterColumn<string>(
71+
name: "Description",
72+
table: "Topics",
73+
type: "TEXT",
74+
nullable: true,
75+
oldClrType: typeof(string),
76+
oldType: "nvarchar(255)");
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)