Note Onion - 4 - API - NoteController

Here is the source code for NoteController.cs This is placed in the Controllers folder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Note.Application.DTOs;
using Note.Application.Services;
using Note.Shared;
using Notes.API;
using Swashbuckle.AspNetCore.Annotations;
using Swashbuckle.AspNetCore.Filters;
using static System.Net.WebRequestMethods;

namespace Notes.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class NoteController : ControllerBase
{
private readonly INoteService _noteService;

public NoteController(INoteService noteService)
{
_noteService = noteService;
}

/// <summary>
/// Returns a list of all notes
/// </summary>
/// <param name="companyKey">This is a code given to your agency</param>
/// <param name="applicationKey">This is a code representing your application</param>
/// <param name="applicationSubKey">This is a code representing an object class in your application</param>
/// <remarks>
/// A result(NoteVM) object containing a collection of
/// note type objects.
///
/// Get: api/v1/Notes
/// </remarks>
[HttpGet("{companyKey}/{applicationKey}/{applicationSubKey}")]
[SwaggerResponse(
StatusCodes.Status200OK,
"HTTP 200 means a model is being returned. Check the WasSuccessfulTrue to determine if it worked properly",
typeof(IResult<List<NoteDto>>))]
[SwaggerResponseExample(
StatusCodes.Status200OK,
typeof(NoteControllerSwashbuckleExamples.NoteGetAllNotesAsync))]
public async Task<IActionResult> GetNotes(
string companyKey,
string applicationKey,
string applicationSubKey)
{
var result = await _noteService.GetAllNotesAsync(companyKey, applicationKey, applicationSubKey);
return Ok(result);
}

//[HttpGet("{id}")]
//[SwaggerResponse(StatusCodes.Status200OK, type: typeof(BaseResult<NoteDto>))]
/// <summary>
/// Returns a note object
/// </summary>
/// <param name="id" example="1">Note.Id</param>
/// <returns>Result containing a NoteViewModel based Result Object</returns>
[HttpGet("{companyKey}/{id}")]
[SwaggerResponse(
StatusCodes.Status200OK,
"HTTP 200 means a model is being returned. Check the WasSuccessfulTrue to determine if it worked properly",
typeof(IResult<NoteDto>))]
[SwaggerResponseExample(
StatusCodes.Status200OK,
typeof(NoteControllerSwashbuckleExamples.NoteGetOneExample))]
public async Task<IActionResult> GetNote(string companyKey, long id)
{
var result = await _noteService.GetNoteById(companyKey, id);
return Ok(result);
}


///// <summary>
///// If you do not pass a number to the GetOne function,
///// then you will get a null model.
///// </summary>
///// <returns></returns>
//[HttpGet("null")]
//[SwaggerResponse(StatusCodes.Status200OK, null, typeof(IResult<NoteDto>))]
////[SwaggerResponseExample(StatusCodes.Status200OK, typeof(SwashbuckleExamples.NoteGetOneExampleNull))]
//public IActionResult GetOne(string companyKey)
//{
// return ReturnResult(new Result<List<NoteDto>>());
//}


//[SwaggerResponse(StatusCodes.Status200OK, type: typeof(BaseResult<long>))]
/// <summary>
/// Used to add a note to the note database
/// </summary>
/// <param name="noteVm">
/// Pass in a NoteDto object
/// </param>
/// <returns></returns>
[HttpPost]
[SwaggerRequestExample(
typeof(NoteDto),
typeof(NoteControllerSwashbuckleExamples.NotePostExample))]
[SwaggerResponse(
StatusCodes.Status200OK,
"HTTP 200 means a model is being returned. Check the WasSuccessfulTrue to determine if it worked properly",
typeof(IResult<NoteDto>))]
[SwaggerResponseExample(
StatusCodes.Status200OK,
typeof(NoteControllerSwashbuckleExamples.NotePostResultsExampleSuccess))]
[SwaggerResponse(
StatusCodes.Status400BadRequest,
"If your Request Body is incorrect, then you'll get 400 error. Look at the errors collection to see the problems.",
typeof(IResult<NoteDto>))]
[SwaggerResponseExample(
StatusCodes.Status400BadRequest,
typeof(NoteControllerSwashbuckleExamples.NotePostResultsExampleError400))]
public async Task<IActionResult> CreateNote([FromBody] NoteDto noteDto)
{
var result = await _noteService.CreateNote(noteDto);
return Ok(result);
}

/// <summary>
/// Used to update an existing note.
/// </summary>
/// <param name="id">
/// This ID of the NoteDto being updated.
/// </param>
/// <param name="noteVm">
/// Pass in a NoteViewModel
/// </param>
/// <returns></returns>
[HttpPut("{id}")]
[SwaggerResponse(
StatusCodes.Status200OK,
type: typeof(BaseResult<bool>))]
[SwaggerRequestExample(
typeof(NoteDto),
typeof(NoteControllerSwashbuckleExamples.NotePutExample))]
[SwaggerResponseExample(StatusCodes.Status200OK,
typeof(NoteControllerSwashbuckleExamples.NotePostResultsExampleSuccess))]
public async Task<IActionResult> UpdateNote(
long id,
[FromBody] NoteDto noteDto)
{
noteDto.Id = id;
var result = await _noteService.UpdateNote(noteDto);
return Ok(result);
}

/// <summary>
/// Used to Delete an existing note
/// </summary>
/// <param name="id">
/// This is the ID of the note that should be deleted
/// </param>
/// <returns></returns>
[HttpDelete("{companyKey}/{id}")]
[SwaggerResponse(
StatusCodes.Status200OK,
"HTTP 200 means a model is being returned. Check the WasSuccessfulTrue to determine if it worked properly",
typeof(IResult<NoteDto>))]
[SwaggerResponseExample(
StatusCodes.Status200OK,
typeof(NoteControllerSwashbuckleExamples.NoteDeleteResultsExampleSuccess))]
public async Task<IActionResult> DeleteNote(string companyKey, long id)
{
var result = await _noteService.DeleteNote(companyKey, id);
return Ok(result);
}
}
}