-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTalentController.cs
More file actions
100 lines (90 loc) · 3.09 KB
/
Copy pathTalentController.cs
File metadata and controls
100 lines (90 loc) · 3.09 KB
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
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using TalentGoWebAPI.Data;
using TalentGoWebAPI.Models;
namespace TalentGoWebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TalentController : ControllerBase
{
public static List<Talents> talents = new List<Talents>
{
//new Talents
//{
// Id = 1,
// FirstName = "Nobert",
// LastName = "Ayesiga",
// Talent = "Developer",
// EmailAddress = "ayesiganobert@gmail.com"
//},
//new Talents {
// Id = 2,
// FirstName = "Jonh",
// LastName="Kim" ,
// EmailAddress="kim@gmail.com"
//},
// new Talents {
// Id = 3,
// FirstName = "Ronald",
// LastName="James" ,
// EmailAddress="kim@gmail.com"
//}
};
private readonly DataContextDB context;
public TalentController(DataContextDB context )
{
this.context = context;
}
[HttpGet]
public async Task<ActionResult<List<Talents>>> Get()
{
return Ok(await context.Talents.ToListAsync());
}
[HttpGet("{id}")]
public async Task<ActionResult<Talents>> Get(int id)
{
var talent = await context.Talents.FindAsync(id);
if (talent == null)
{
return BadRequest("Talent not found");
}
return Ok(talent);
}
[HttpPost]
public async Task<ActionResult<List<Talents>>> AddTalent(Talents talent)
{
context.Talents.Add(talent);
await context.SaveChangesAsync();
return Ok(await context.Talents.ToListAsync());
}
[HttpPut]
public async Task<ActionResult<List<Talents>>> UpdateTalent(Talents talentrqst)
{
var dbtalent = await context.Talents.FindAsync(talentrqst.Id);
if (dbtalent == null)
{
return BadRequest("Talent not found");
}
dbtalent.FirstName = talentrqst.FirstName;
dbtalent.LastName = talentrqst.LastName;
dbtalent.Talent = talentrqst.Talent;
dbtalent.EmailAddress = talentrqst.EmailAddress;
await context.SaveChangesAsync();
return Ok(await context.Talents.ToListAsync());
}
[HttpDelete("{id}")]
public async Task<ActionResult<Talents>> DeleteTalent(int id)
{
var dbtalent = await context.Talents.FindAsync(id);
if (dbtalent == null)
{
return BadRequest("Talent Doesn't Exist");
}
context.Talents.Remove(dbtalent);
await context.SaveChangesAsync();
return Ok(await context.Talents.ToListAsync());
}
}
}