Skip to content

Commit f0a20f4

Browse files
Complete Controller, Entity and Dto
1 parent ea4a138 commit f0a20f4

26 files changed

+794
-58
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System.ComponentModel;
3+
using taskmaster_api.Data.DTOs;
4+
using taskmaster_api.Data.DTOs.Interface;
5+
6+
namespace taskmaster_api.Controllers
7+
{
8+
public class ApplicationControllerBase : ControllerBase
9+
{
10+
public ApplicationControllerBase() { }
11+
12+
protected IActionResult ToHttpResult(ICoreActionResult coreActionResult)
13+
{
14+
if (coreActionResult.IsSuccess)
15+
{
16+
return Ok();
17+
}
18+
19+
return BadRequest(ToExceptionDto(coreActionResult));
20+
}
21+
22+
protected IActionResult ToHttpResult<T>(ICoreActionResult<T> coreActionResult)
23+
{
24+
if (coreActionResult.IsSuccess)
25+
{
26+
return Ok(coreActionResult.Data);
27+
}
28+
29+
return BadRequest(ToExceptionDto(coreActionResult));
30+
}
31+
32+
private CoreExceptionDto ToExceptionDto<T>(ICoreActionResult<T> result)
33+
{
34+
CoreExceptionDto e = new CoreExceptionDto();
35+
36+
e.errorCode = result.ErrorCode;
37+
e.errorMessage = result.ErrorMessage;
38+
e.suppressToastMessage = false;
39+
e.stackTrace = String.Empty;
40+
41+
return e;
42+
}
43+
44+
private CoreExceptionDto ToExceptionDto(ICoreActionResult result)
45+
{
46+
CoreExceptionDto e = new CoreExceptionDto();
47+
48+
e.errorCode = result.ErrorCode;
49+
e.errorMessage = result.ErrorMessage;
50+
e.suppressToastMessage = false;
51+
e.suppressSideBarAlert = false;
52+
e.stackTrace = String.Empty;
53+
54+
return e;
55+
}
56+
}
57+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Http.HttpResults;
3+
using Microsoft.AspNetCore.Mvc;
4+
using taskmaster_api.Data.DTOs;
5+
using taskmaster_api.Data.DTOs.Interface;
6+
using taskmaster_api.Data.Entities;
7+
using taskmaster_api.Data.Repositories.Interface;
8+
using taskmaster_api.Services;
9+
using taskmaster_api.Services.Interface;
10+
11+
namespace taskmaster_api.Controllers
12+
{
13+
[Route("api/[controller]")]
14+
[ApiController]
15+
public class TaskController : ApplicationControllerBase
16+
{
17+
private readonly ITaskAppService _taskAppService;
18+
19+
public TaskController(ITaskAppService taskAppService)
20+
{
21+
_taskAppService = taskAppService;
22+
}
23+
24+
[HttpGet("{id}")]
25+
public IActionResult GetTask(int id)
26+
{
27+
return ToHttpResult<TaskDto>(_taskAppService.GetTaskById(id));
28+
}
29+
30+
[HttpPost]
31+
public IActionResult CreateTask(TaskDto taskDto)
32+
{
33+
return ToHttpResult<TaskDto>(_taskAppService.CreateTask(taskDto));
34+
}
35+
36+
[HttpPut("{id}")]
37+
public IActionResult UpdateTask(int id, TaskDto taskDto)
38+
{
39+
return ToHttpResult<TaskDto>(_taskAppService.UpdateTask(id, taskDto));
40+
}
41+
42+
[HttpDelete("{id}")]
43+
public IActionResult DeleteTask(int id)
44+
{
45+
return ToHttpResult(_taskAppService.DeleteTask(id));
46+
}
47+
}
48+
}

taskmaster-api/Controllers/WeatherForecastController.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

taskmaster-api/Data/Contexts/ApplicationDbContext.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.EntityFrameworkCore;
2+
using taskmaster_api.Data.Entities;
23

34
namespace taskmaster_api.Data.Contexts
45
{
@@ -7,5 +8,8 @@ public class ApplicationDbContext : DbContext
78
public ApplicationDbContext(DbContextOptions options) : base(options)
89
{
910
}
11+
12+
public virtual DbSet<UserEntity> Users { get; set; }
13+
public virtual DbSet<TaskEntity> Tasks { get; set; }
1014
}
1115
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using taskmaster_api.Data.DTOs.Interface;
2+
3+
namespace taskmaster_api.Data.DTOs
4+
{
5+
public class CoreActionResult : ICoreActionResult
6+
{
7+
protected CoreActionResult(bool isSucess, bool isException, string message, string errorCode)
8+
{
9+
this.IsSuccess = isSucess;
10+
this.IsException = isException;
11+
this.ErrorMessage = message;
12+
this.ErrorCode = errorCode;
13+
}
14+
15+
protected CoreActionResult(bool isSucess, string message, string errorCode)
16+
{
17+
this.IsException = false;
18+
this.IsSuccess = isSucess;
19+
this.ErrorMessage = message;
20+
this.ErrorCode = errorCode;
21+
}
22+
23+
/// <summary>
24+
/// Error message that can be sent to the Client
25+
/// </summary>
26+
public string ErrorMessage { get; }
27+
public string ErrorCode { get; }
28+
public bool IsFailure => IsSuccess == false;
29+
public bool IsSuccess { get; }
30+
public bool IsException { get; set; }
31+
public static ICoreActionResult Failure(
32+
string errorMesage,
33+
string errorCode)
34+
{
35+
return new CoreActionResult(false, errorMesage, errorCode);
36+
}
37+
38+
public static ICoreActionResult Exception(Exception ex)
39+
{
40+
return new CoreActionResult(
41+
false,
42+
true,
43+
ex.Message,
44+
"COMMON.UNHANDLED_EXCEPTION");
45+
}
46+
47+
public static ICoreActionResult Success()
48+
{
49+
return new CoreActionResult(true, String.Empty, String.Empty);
50+
}
51+
52+
public static ICoreActionResult Ignore(string errorMesage, string errorCode)
53+
{
54+
return new CoreActionResult(false, errorMesage, errorCode);
55+
}
56+
57+
}
58+
59+
public class CoreActionResult<T> : CoreActionResult, ICoreActionResult<T>
60+
{
61+
public CoreActionResult(bool isSuccess, string errorMesage, T data, string errorCode)
62+
: base(isSuccess, errorMesage, errorCode)
63+
{
64+
Data = data;
65+
}
66+
67+
public T Data { get; }
68+
69+
public static ICoreActionResult<T> Failure(string errorMesage)
70+
{
71+
return new CoreActionResult<T>(false, errorMesage, default(T), "");
72+
}
73+
74+
public static ICoreActionResult<T> Exception(Exception ex)
75+
{
76+
return new CoreActionResult<T>(
77+
false,
78+
ex.Message,
79+
default(T),
80+
"COMMON.UNHANDLED_EXCEPTION");
81+
}
82+
83+
84+
public static ICoreActionResult<T> Failure(string errorMessage, string errorCode)
85+
{
86+
return new CoreActionResult<T>(false, errorMessage, default(T), errorCode);
87+
}
88+
89+
public static ICoreActionResult<T> Success(T data)
90+
{
91+
return new CoreActionResult<T>(true, string.Empty, data, string.Empty);
92+
}
93+
}
94+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace taskmaster_api.Data.DTOs
2+
{
3+
public class CoreExceptionDto
4+
{
5+
public CoreExceptionDto()
6+
{
7+
this.errorCode = "COMMON.UNHANDLED_EXCEPTION";
8+
this.errorMessage = String.Empty;
9+
this.stackTrace = String.Empty;
10+
this.suppressToastMessage = false;
11+
this.suppressSideBarAlert = false;
12+
}
13+
public string errorMessage { get; set; }
14+
public string errorCode { get; set; }
15+
public string stackTrace { get; set; }
16+
public bool suppressToastMessage { get; set; }
17+
public bool suppressSideBarAlert { get; set; }
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace taskmaster_api.Data.DTOs.Interface
2+
{
3+
public interface ICoreActionResult
4+
{
5+
bool IsSuccess { get; }
6+
string ErrorMessage { get; }
7+
string ErrorCode { get; }
8+
}
9+
10+
public interface ICoreActionResult<T> : ICoreActionResult
11+
{
12+
T Data { get; }
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace taskmaster_api.Data.DTOs.Interface
2+
{
3+
public interface IDto<TDestination>
4+
{
5+
TDestination ToEntity();
6+
}
7+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using taskmaster_api.Data.DTOs.Interface;
2+
using taskmaster_api.Data.Entities;
3+
using taskmaster_api.Utilities;
4+
5+
namespace taskmaster_api.Data.DTOs
6+
{
7+
public class TaskDto : IDto<TaskEntity>
8+
{
9+
public int Id { get; set; }
10+
public string Title { get; set; }
11+
public string Description { get; set; }
12+
public DateTime DueDate { get; set; }
13+
14+
public TaskEntity ToEntity()
15+
{
16+
return DtoHelpers.ToEntity<TaskDto, TaskEntity>(this);
17+
}
18+
}
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace taskmaster_api.Data.DTOs
2+
{
3+
public class UserDto
4+
{
5+
}
6+
}

0 commit comments

Comments
 (0)