-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathUsersController.cs
More file actions
104 lines (86 loc) · 2.56 KB
/
UsersController.cs
File metadata and controls
104 lines (86 loc) · 2.56 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
101
102
103
104
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json.Linq;
using dvcsharp_core_api.Models;
using dvcsharp_core_api.Data;
namespace dvcsharp_core_api
{
[Route("api/[controller]")]
public class UsersController : Controller
{
private readonly GenericDataContext _context;
public UsersController(GenericDataContext context)
{
_context = context;
}
[Authorize]
[HttpGet]
public IEnumerable<User> Get()
{
return _context.Users.ToList();
}
[Authorize]
[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody] Models.UserUpdateRequest user)
{
if(!ModelState.IsValid) {
return BadRequest(ModelState);
}
//var existingUser = _context.Users.SingleOrDefault(m => m.ID == id);
var existingUser = _context.Users.GetById(id);
if(existingUser == null) {
return NotFound();
}
existingUser.name = user.name;
existingUser.email = user.email;
existingUser.role = user.role;
existingUser.updatePassword(user.password);
_context.Users.Update(existingUser);
_context.SaveChanges();
return Ok(existingUser);
}
[Authorize]
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
User user = _context.Users.SingleOrDefault(m => m.ID == id);
if(user == null) {
return NotFound();
}
_context.Users.Remove(user);
_context.SaveChanges();
return Ok(user);
}
[Authorize]
[HttpGet("import")]
public async Task<IActionResult> Import()
{
HttpClient client = new HttpClient();
var url = HttpContext.Request.Query["url"].ToString();
string responseBody = null;
string errorMsg = "Success";
try {
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
responseBody = await response.Content.ReadAsStringAsync();
MockUserImport(responseBody);
}
catch(Exception e) {
errorMsg = e.Message;
}
return Ok(new JObject(
new JProperty("Error", errorMsg),
new JProperty("Content", responseBody)
));
}
private void MockUserImport(string data)
{
// Mock
}
}
}