提交 5673d1e6 编写于 作者: 麦壳饼's avatar 麦壳饼

add other api

上级 3d1e790e
......@@ -64,58 +64,6 @@ namespace IoTSharp.Hub.Controllers
return actionResult;
}
[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> Install([FromBody] InstallDto model)
{
IActionResult actionResult = NoContent();
try
{
var tenant = new Tenant() { Id = Guid.NewGuid(), Name = model.TenantName, EMail = model.TenantEMail };
var customer = new Customer() { Id = Guid.NewGuid(), Name = model.CustomerName, Email = model.CustomerEMail };
customer.Tenant = tenant;
tenant.Customers = new List<Customer>();
tenant.Customers.Add(customer);
tenant.Customers.Add(customer);
var user = new IdentityUser
{
Email = model.AdminEmail,
UserName = model.UserName,
PhoneNumber = model.PhoneNumber
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
_context.Tenant.Add(tenant);
_context.Customer.Add(customer);
await _signInManager.SignInAsync(user, false);
await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.Email, model.AdminEmail));
var cust = _context.Customer.FirstOrDefault(c => c.Name == model.CustomerName);
if (cust != null)
{
await _signInManager.UserManager.AddClaimAsync(user, new Claim(ClaimTypes.GroupSid, cust.Id.ToString()));
actionResult = Ok(new { code = 0, msg = "OK", data = GenerateJwtToken(model.AdminEmail, user) });
}
}
else
{
var msg = from e in result.Errors select $"{e.Code}:{e.Description}\r\n";
actionResult = BadRequest(new { code = -3, msg = string.Join(';', msg.ToArray()) });
}
}
catch (Exception ex)
{
actionResult = BadRequest(new { code = -2, msg = ex.Message, data = ex });
_logger.LogError(ex, ex.Message);
}
return actionResult;
}
/// <summary>
/// Register a user
/// </summary>
......@@ -193,35 +141,19 @@ namespace IoTSharp.Hub.Controllers
[Required]
public string Email { get; set; }
}
public class RegisterDto
{
[Required]
public string Email { get; set; }
[Required]
public string CustomerName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "PASSWORD_MIN_LENGTH", MinimumLength = 6)]
public string Password { get; set; }
}
public class InstallDto
{
[Required]
public string AdminEmail { get; set; }
[Required]
public string CustomerName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "PASSWORD_MIN_LENGTH", MinimumLength = 6)]
public string Password { get; set; }
public string TenantName { get; set; }
public string TenantEMail { get; set; }
public string CustomerEMail { get; set; }
public string UserName { get; internal set; }
public string PhoneNumber { get; internal set; }
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using IoTSharp.Hub.Data;
namespace IoTSharp.Hub.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CustomersController : ControllerBase
{
private readonly ApplicationDbContext _context;
public CustomersController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/Customers
[HttpGet]
public async Task<ActionResult<IEnumerable<Customer>>> GetCustomer()
{
return await _context.Customer.ToListAsync();
}
// GET: api/Customers/5
[HttpGet("{id}")]
public async Task<ActionResult<Customer>> GetCustomer(Guid id)
{
var customer = await _context.Customer.FindAsync(id);
if (customer == null)
{
return NotFound();
}
return customer;
}
// PUT: api/Customers/5
[HttpPut("{id}")]
public async Task<IActionResult> PutCustomer(Guid id, Customer customer)
{
if (id != customer.Id)
{
return BadRequest();
}
_context.Entry(customer).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CustomerExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Customers
[HttpPost]
public async Task<ActionResult<Customer>> PostCustomer(Customer customer)
{
_context.Customer.Add(customer);
await _context.SaveChangesAsync();
return CreatedAtAction("GetCustomer", new { id = customer.Id }, customer);
}
// DELETE: api/Customers/5
[HttpDelete("{id}")]
public async Task<ActionResult<Customer>> DeleteCustomer(Guid id)
{
var customer = await _context.Customer.FindAsync(id);
if (customer == null)
{
return NotFound();
}
_context.Customer.Remove(customer);
await _context.SaveChangesAsync();
return customer;
}
private bool CustomerExists(Guid id)
{
return _context.Customer.Any(e => e.Id == id);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using IoTSharp.Hub.Data;
namespace IoTSharp.Hub.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DevicesController : ControllerBase
{
private readonly ApplicationDbContext _context;
public DevicesController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/Devices
[HttpGet]
public async Task<ActionResult<IEnumerable<Device>>> GetDevice()
{
return await _context.Device.ToListAsync();
}
// GET: api/Devices/5
[HttpGet("{id}")]
public async Task<ActionResult<Device>> GetDevice(Guid id)
{
var device = await _context.Device.FindAsync(id);
if (device == null)
{
return NotFound();
}
return device;
}
// PUT: api/Devices/5
[HttpPut("{id}")]
public async Task<IActionResult> PutDevice(Guid id, Device device)
{
if (id != device.Id)
{
return BadRequest();
}
_context.Entry(device).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!DeviceExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Devices
[HttpPost]
public async Task<ActionResult<Device>> PostDevice(Device device)
{
_context.Device.Add(device);
await _context.SaveChangesAsync();
return CreatedAtAction("GetDevice", new { id = device.Id }, device);
}
// DELETE: api/Devices/5
[HttpDelete("{id}")]
public async Task<ActionResult<Device>> DeleteDevice(Guid id)
{
var device = await _context.Device.FindAsync(id);
if (device == null)
{
return NotFound();
}
_context.Device.Remove(device);
await _context.SaveChangesAsync();
return device;
}
private bool DeviceExists(Guid id)
{
return _context.Device.Any(e => e.Id == id);
}
}
}
......@@ -79,7 +79,7 @@ namespace IoTSharp.Hub.Controllers
var user = new IdentityUser
{
Email = model.Email,
UserName = model.UserName,
UserName = model.Email,
PhoneNumber = model.PhoneNumber
};
......@@ -144,8 +144,6 @@ namespace IoTSharp.Hub.Controllers
[EmailAddress]
public string CustomerEMail { get; set; }
public string UserName { get; set; }
[Phone]
public string PhoneNumber { get; set; }
}
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using IoTSharp.Hub.Data;
namespace IoTSharp.Hub.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TenantsController : ControllerBase
{
private readonly ApplicationDbContext _context;
public TenantsController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/Tenants
[HttpGet]
public async Task<ActionResult<IEnumerable<Tenant>>> GetTenant()
{
return await _context.Tenant.ToListAsync();
}
// GET: api/Tenants/5
[HttpGet("{id}")]
public async Task<ActionResult<Tenant>> GetTenant(Guid id)
{
var tenant = await _context.Tenant.FindAsync(id);
if (tenant == null)
{
return NotFound();
}
return tenant;
}
// PUT: api/Tenants/5
[HttpPut("{id}")]
public async Task<IActionResult> PutTenant(Guid id, Tenant tenant)
{
if (id != tenant.Id)
{
return BadRequest();
}
_context.Entry(tenant).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TenantExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Tenants
[HttpPost]
public async Task<ActionResult<Tenant>> PostTenant(Tenant tenant)
{
_context.Tenant.Add(tenant);
await _context.SaveChangesAsync();
return CreatedAtAction("GetTenant", new { id = tenant.Id }, tenant);
}
// DELETE: api/Tenants/5
[HttpDelete("{id}")]
public async Task<ActionResult<Tenant>> DeleteTenant(Guid id)
{
var tenant = await _context.Tenant.FindAsync(id);
if (tenant == null)
{
return NotFound();
}
_context.Tenant.Remove(tenant);
await _context.SaveChangesAsync();
return tenant;
}
private bool TenantExists(Guid id)
{
return _context.Tenant.Any(e => e.Id == id);
}
}
}
......@@ -43,6 +43,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
<PackageReference Include="MQTTnet" Version="2.8.5" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.0" />
<PackageReference Include="NSwag.AspNetCore" Version="12.0.7" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册