Skip to content

Commit 8e500d6

Browse files
committed
Changed some services
1 parent 647793a commit 8e500d6

File tree

12 files changed

+260
-59
lines changed

12 files changed

+260
-59
lines changed

.vscode/launch.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
// Use IntelliSense to find out which attributes exist for C# debugging
6+
// Use hover for the description of the existing attributes
7+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
8+
"name": ".NET Core Launch (web)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/net6.0/QuizApi.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
"stopAtEntry": false,
17+
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
18+
"serverReadyAction": {
19+
"action": "openExternally",
20+
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
21+
},
22+
"env": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
},
25+
"sourceFileMap": {
26+
"/Views": "${workspaceFolder}/Views"
27+
}
28+
},
29+
{
30+
"name": ".NET Core Attach",
31+
"type": "coreclr",
32+
"request": "attach"
33+
}
34+
]
35+
}

.vscode/tasks.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/QuizApi.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/QuizApi.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/QuizApi.csproj"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}

Controllers/TopicsController.cs

+52-15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ public TopicsController(
2121
_service = service;
2222
}
2323

24+
[HttpPost]
25+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
26+
public IActionResult PostTopic(CreateTopicDto dtoModel)
27+
{
28+
if(!ModelState.IsValid) return NotFound();
29+
30+
var model = Mappers.DtoToModel(dtoModel);
31+
32+
var result = _service.Create(model);
33+
34+
if(!result.IsSuccess)
35+
{
36+
_logger.LogInformation($" 🛑 Reason of 📧 exception is {result.exception?.Message}");
37+
return BadRequest();
38+
}
39+
40+
return Created("/", dtoModel);
41+
}
42+
2443
[HttpGet]
2544
[Produces("application/json")]
2645
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(ResponseBase<List<Topic>>))]
@@ -42,29 +61,47 @@ public IActionResult GetTopics([FromQuery]Pagination pagination)
4261
return Ok(response);
4362
}
4463

45-
[HttpPost]
46-
[ProducesResponseType(StatusCodes.Status400BadRequest)]
47-
public async Task<IActionResult> PostTopic(CreateTopicDto model)
64+
[HttpGet("{id}")]
65+
public IActionResult GetTopicById([FromRoute]ulong id)
4866
{
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}");
67+
if(!_service.TopicExists(id)) return NotFound("Topic not found");
5568

56-
69+
var result = _service.GetById(id);
5770
if(!result.IsSuccess)
5871
{
59-
_logger.LogInformation($"Error boldi sababi: {result.exception?.Message}");
72+
_logger.LogInformation($" 🛑 Reason of 📧 exception is {result.exception?.Message}");
6073
return BadRequest();
6174
}
62-
// var entity = ToEntity(model);
75+
return Ok(result.topic);
76+
}
6377

64-
// var result = await _unitOfWork.Topics.Add(entity);
65-
// _unitOfWork.Save();
78+
[HttpPut("{id}")]
79+
public IActionResult UpdateTopic([FromRoute]ulong id, [FromForm]UpdateTopicDto dtoModel)
80+
{
81+
if(!_service.TopicExists(id)) return NotFound("Topic not found");
82+
83+
var model = Mappers.UpdateDtoToModel(dtoModel);
84+
var result = _service.Update(model);
85+
if(!result.IsSuccess)
86+
{
87+
_logger.LogInformation($" 🛑 Reason of 📧 exception is {result.exception?.Message}");
88+
return BadRequest();
89+
}
90+
return Accepted(result.topic);
91+
}
6692

93+
[HttpDelete("{id}")]
94+
public IActionResult DeleteTopic([FromQuery]ulong id)
95+
{
96+
if(!_service.TopicExists(id)) return NotFound("Topic not found");
6797

68-
return Created("/", model);
98+
var model = _service.GetById(id);
99+
var result = _service.Remove(model.topic);
100+
if(!result.IsSuccess)
101+
{
102+
_logger.LogInformation($" 🛑 Reason of 📧 exception is {result.exception?.Message}");
103+
return BadRequest();
104+
}
105+
return Accepted(result.topic);
69106
}
70107
}

Dtos/Topic/Topic.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ public class Topic
1010
public DateTimeOffset CreatedAt { get; set; }
1111
public DateTimeOffset UpdatedAt { get; set; }
1212

13-
[Obsolete("This constructor is only used by EF Core",true)]
14-
public Topic(){}
13+
// [Obsolete("This constructor is only used by EF Core",true)]
14+
// public Topic(){}
1515

16-
public Topic(ulong id, string? name, string? description, string? difficulty, DateTimeOffset createdAt, DateTimeOffset updatedAt)
17-
{
18-
Id = id;
19-
Name = name;
20-
Description = description;
21-
Difficulty = difficulty;
22-
CreatedAt = createdAt;
23-
UpdatedAt = updatedAt;
24-
}
16+
// public Topic(ulong id, string? name, string? description, string? difficulty, DateTimeOffset createdAt, DateTimeOffset updatedAt)
17+
// {
18+
// Id = id;
19+
// Name = name;
20+
// Description = description;
21+
// Difficulty = difficulty;
22+
// CreatedAt = createdAt;
23+
// UpdatedAt = updatedAt;
24+
// }
2525
}

Dtos/Topic/UpdateTopicDto.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace QuizApi.Dtos.Topic;
4+
5+
public class UpdateTopicDto
6+
{
7+
[Required, MaxLength(50)]
8+
public string? Name { get; set; }
9+
10+
[Required, MaxLength(255)]
11+
public string? Description { get; set; }
12+
public ETopicDifficulty Difficulty { get; set; }
13+
14+
15+
}

Repositories/GenericRepository.cs

+6
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ public TEntity Remove(TEntity entity)
3939

4040
public void RemoveRange(IEnumerable<TEntity> entities)
4141
=> _context.Set<TEntity>().RemoveRange(entities);
42+
43+
public TEntity Update(TEntity entity)
44+
{
45+
var entry = _context.Set<TEntity>().Update(entity);
46+
return entry.Entity;
47+
}
4248
}

Repositories/IGenericRepository.cs

+2
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public interface IGenericRepository<TEntity> where TEntity : class
1111
void AddRange(IEnumerable<TEntity> entities);
1212
TEntity Remove(TEntity entity);
1313
void RemoveRange(IEnumerable<TEntity> entities);
14+
TEntity Update(TEntity entity);
15+
1416
}

Services/ITopicService.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ namespace QuizApi.Services;
22

33
public interface ITopicService
44
{
5+
(bool IsSuccess, Exception? exception, Models.Topic topic) Create(Models.Topic topic);
56
IEnumerable<Models.Topic> GetAll();
6-
ValueTask<Models.Topic> GetByIdAsync(ulong id);
7-
ValueTask<Models.Topic> GetByNameAsync(string name);
8-
ValueTask<Models.Topic> RemoveAsync(Models.Topic topic);
9-
Task<(bool IsSuccess, Exception? exception, Models.Topic topic)> CreateAsync(Models.Topic topic);
10-
ValueTask<Models.Topic> UpdateAsync(Models.Topic topic);
7+
(bool IsSuccess, Exception? exception, Models.Topic topic) GetById(ulong id);
8+
(bool IsSuccess, Exception? exception, IEnumerable<Models.Topic>) GetByName(string name);
9+
(bool IsSuccess, Exception? exception, Models.Topic topic) Update(Models.Topic topic);
10+
(bool IsSuccess, Exception? exception, Models.Topic topic) Remove(Models.Topic topic);
11+
12+
bool TopicExists(ulong id);
1113
}

Services/Mappers/Mapper.cs

+24-8
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,32 @@ public static Models.Topic DtoToModel(CreateTopicDto dtoModel)
3434
return model;
3535
}
3636

37-
public static Topic ModelToDto(Models.Topic model)
37+
public static Models.Topic UpdateDtoToModel(UpdateTopicDto dtoModel)
3838
{
39-
var dtoModel = new Topic(
40-
id: model.Id,
41-
name: model.Name,
42-
description: model.Description,
43-
difficulty: model.Difficulty.ToString(),
44-
createdAt: model.CreatedAt,
45-
updatedAt: model.UpdatedAt
39+
40+
var model = new Models.Topic(
41+
name: dtoModel.Name!,
42+
description: dtoModel.Description!,
43+
difficulty: dtoModel.Difficulty switch
44+
{
45+
Dtos.Topic.ETopicDifficulty.Beginner => Models.Enums.ETopicDifficulty.Beginner,
46+
Dtos.Topic.ETopicDifficulty.Intermediate => Models.Enums.ETopicDifficulty.Intermediate,
47+
_ => Models.Enums.ETopicDifficulty.Advanced
48+
}
4649
);
50+
return model;
51+
}
52+
53+
public static Topic ModelToDto(Models.Topic model)
54+
{
55+
var dtoModel = new Topic(){
56+
Id = model.Id,
57+
Name = model.Name,
58+
Description = model.Description,
59+
Difficulty = model.Difficulty.ToString(),
60+
CreatedAt = model.CreatedAt,
61+
UpdatedAt = model.UpdatedAt
62+
};
4763
return dtoModel;
4864
}
4965

0 commit comments

Comments
 (0)