save locations

This commit is contained in:
hydrant
2019-09-29 22:16:59 +02:00
parent cf7fccbc28
commit 6a23f6ef59
17 changed files with 1607 additions and 49 deletions

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ReallifeGamemode.Database.Models;
namespace ReallifeGamemode.DataService.Controllers
{
[ApiController]
[Authorize]
[Produces("application/json")]
[Route("DataService/[controller]")]
public class LocationController : ControllerBase
{
private readonly DatabaseContext dbContext;
public LocationController(DatabaseContext dbContext)
{
this.dbContext = dbContext;
}
[HttpGet("GetLocations")]
public ActionResult<IEnumerable<string>> GetLocations()
{
var list = this.dbContext.Locations.OrderByDescending(l => l.Id);
List<string> locations = new List<string>();
foreach(var location in list)
{
locations.Add($"{location.Description.PadRight(20)} - {Math.Round(location.X, 2).ToString(CultureInfo.InvariantCulture)}, {Math.Round(location.Y, 2).ToString(CultureInfo.InvariantCulture)}, {Math.Round(location.Z, 2).ToString(CultureInfo.InvariantCulture)}");
}
return locations;
}
}
}

View File

@@ -15,7 +15,7 @@ namespace ReallifeGamemode.DataService.Controllers
{
[ApiController]
[Authorize]
[Route("DataService/User")]
[Route("DataService/[controller]")]
[Produces("application/json")]
public class UserController : ControllerBase
{

View File

@@ -14,10 +14,6 @@
<None Remove="logs\**" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.11" />

View File

@@ -131,6 +131,8 @@ namespace ReallifeGamemode.DataService
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseCors(c =>
{
c.AllowAnyOrigin()

View File

@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Orte</title>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<label for="username">Username</label>
<input type="text" id="username" />
<label for="password">Passwort</label>
<input type="password" id="password" />
<input type="button" value="Anmelden" id="login-btn" />
<span id="login-status"></span>
<br />
<input type="button" value="Gespeicherte Orte laden" id="fetch-locations-btn" />
<br />
<ul id="location-list"></ul>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var token = null;
var list = $("#location-list");
$("#login-btn").click(function () {
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
url: 'DataService/Auth/Login',
type: 'post',
contentType: 'application/json',
data: JSON.stringify({
username: username,
password: password
})
}).done(function (result) {
token = result.token;
$("#login-status").text("Erfolgreich angemeldet");
}).fail(function () {
$("#login-status").text("Login fehlgeschlagen");
});
});
$("#fetch-locations-btn").click(function () {
list.empty();
$.ajax({
url: 'DataService/Location/GetLocations',
type: 'get',
headers: {
Authorization: 'Bearer ' + token
}
}).done(function (result) {
result.forEach(function (item) {
list.append(`<li>${item}</li>`);
});
});
});
});
</script>
</body>
</html>