Skip to content
This repository was archived by the owner on Jan 3, 2022. It is now read-only.

Commit 0a3fdef

Browse files
authored
Add files via upload
1 parent 352ed4e commit 0a3fdef

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.EntityFrameworkCore;
6+
using BackEnd2.Models;
7+
8+
namespace BackEnd2.Controllers
9+
{
10+
[Route("api/[controller]")]
11+
[ApiController]
12+
public class TodoItemsController : ControllerBase
13+
{
14+
private readonly TodoContext _context;
15+
16+
public TodoItemsController(TodoContext context)
17+
{
18+
_context = context;
19+
}
20+
21+
// GET: api/TodoItems
22+
[HttpGet]
23+
public async Task<ActionResult<IEnumerable<TodoItemDTO>>> GetTodoItems()
24+
{
25+
return await _context.TodoItems
26+
.Select(x => ItemToDTO(x))
27+
.ToListAsync();
28+
}
29+
30+
31+
// GET: api/TodoItems/5
32+
[HttpGet("{id}")]
33+
public async Task<ActionResult<TodoItemDTO>> GetTodoItem(long id)
34+
{
35+
var todoItem = await _context.TodoItems.FindAsync(id);
36+
37+
if (todoItem == null)
38+
{
39+
return NotFound();
40+
}
41+
42+
return ItemToDTO(todoItem);
43+
}
44+
45+
// PUT: api/TodoItems/5
46+
// To protect from overposting attacks, enable the specific properties you want to bind to, for
47+
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
48+
[HttpPut("{id}")]
49+
public async Task<IActionResult> UpdateTodoItem(long id, TodoItemDTO todoItemDTO)
50+
{
51+
if (id != todoItemDTO.Id)
52+
{
53+
return BadRequest();
54+
}
55+
56+
var todoItem = await _context.TodoItems.FindAsync(id);
57+
if (todoItem == null)
58+
{
59+
return NotFound();
60+
}
61+
62+
todoItem.Name = todoItemDTO.Name;
63+
todoItem.IsComplete = todoItemDTO.IsComplete;
64+
65+
try
66+
{
67+
await _context.SaveChangesAsync();
68+
}
69+
catch (DbUpdateConcurrencyException) when (!TodoItemExists(id))
70+
{
71+
return NotFound();
72+
}
73+
74+
return NoContent();
75+
}
76+
77+
// POST: api/TodoItems
78+
// To protect from overposting attacks, enable the specific properties you want to bind to, for
79+
// more details, see https://go.microsoft.com/fwlink/?linkid=2123754.
80+
[HttpPost]
81+
public async Task<ActionResult<TodoItemDTO>> CreateTodoItem(TodoItemDTO todoItemDTO)
82+
{
83+
var todoItem = new TodoItem
84+
{
85+
IsComplete = todoItemDTO.IsComplete,
86+
Name = todoItemDTO.Name
87+
};
88+
89+
_context.TodoItems.Add(todoItem);
90+
await _context.SaveChangesAsync();
91+
92+
return CreatedAtAction(
93+
nameof(GetTodoItem),
94+
new { id = todoItem.Id },
95+
ItemToDTO(todoItem));
96+
}
97+
98+
// DELETE: api/TodoItems/5
99+
[HttpDelete("{id}")]
100+
public async Task<IActionResult> DeleteTodoItem(long id)
101+
{
102+
var todoItem = await _context.TodoItems.FindAsync(id);
103+
104+
if (todoItem == null)
105+
{
106+
return NotFound();
107+
}
108+
109+
_context.TodoItems.Remove(todoItem);
110+
await _context.SaveChangesAsync();
111+
112+
return NoContent();
113+
}
114+
115+
116+
private bool TodoItemExists(long id) =>
117+
_context.TodoItems.Any(e => e.Id == id);
118+
119+
private static TodoItemDTO ItemToDTO(TodoItem todoItem) =>
120+
new TodoItemDTO
121+
{
122+
Id = todoItem.Id,
123+
Name = todoItem.Name,
124+
IsComplete = todoItem.IsComplete
125+
};
126+
}
127+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.Extensions.Logging;
7+
8+
namespace BackEnd2.Controllers
9+
{
10+
[ApiController]
11+
[Route("[controller]")]
12+
public class WeatherForecastController : ControllerBase
13+
{
14+
private static readonly string[] Summaries = new[]
15+
{
16+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
17+
};
18+
19+
private readonly ILogger<WeatherForecastController> _logger;
20+
21+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
22+
{
23+
_logger = logger;
24+
}
25+
26+
[HttpGet]
27+
public IEnumerable<WeatherForecast> Get()
28+
{
29+
var rng = new Random();
30+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
31+
{
32+
Date = DateTime.Now.AddDays(index),
33+
TemperatureC = rng.Next(-20, 55),
34+
Summary = Summaries[rng.Next(Summaries.Length)]
35+
})
36+
.ToArray();
37+
}
38+
}
39+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace BackEnd2.Models
4+
{
5+
public class TodoContext : DbContext
6+
{
7+
public TodoContext(DbContextOptions<TodoContext> options)
8+
: base(options)
9+
{
10+
}
11+
public DbSet<TodoItem> TodoItems { get; set; }
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace BackEnd2.Models
2+
{
3+
public class TodoItem
4+
{
5+
public long Id { get; set; }
6+
public string Name { get; set; }
7+
public bool IsComplete { get; set; }
8+
public string Secret { get; set; }
9+
}
10+
11+
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BackEnd2.Models
2+
{
3+
public class TodoItemDTO
4+
{
5+
public long Id { get; set; }
6+
public string Name { get; set; }
7+
public bool IsComplete { get; set; }
8+
}
9+
}

0 commit comments

Comments
 (0)