Skip to content

Commit 11024fc

Browse files
committed
chore: cleanup (global) VariablesController
1 parent 9a2fcf3 commit 11024fc

File tree

1 file changed

+78
-65
lines changed

1 file changed

+78
-65
lines changed
Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Intersect.GameObjects;
1+
using Intersect.Enums;
2+
using Intersect.GameObjects;
23
using Intersect.Server.Database;
34
using Intersect.Server.Database.GameData;
45
using Intersect.Server.Entities;
@@ -7,98 +8,110 @@
78
using Microsoft.AspNetCore.Authorization;
89
using Microsoft.AspNetCore.Mvc;
910

10-
namespace Intersect.Server.Web.RestApi.Routes.V1
11+
namespace Intersect.Server.Web.RestApi.Routes.V1;
12+
13+
[Route("api/v1/variables/global")]
14+
[Authorize]
15+
public sealed partial class VariablesController : IntersectController
1116
{
12-
[Route("api/v1/variables/global")]
13-
[Authorize]
14-
public sealed partial class VariablesController : IntersectController
17+
[HttpGet]
18+
public IActionResult GlobalVariablesGet([FromQuery] PagingInfo pageInfo)
1519
{
16-
[HttpGet]
17-
public object GlobalVariablesGet([FromQuery] PagingInfo pageInfo)
18-
{
19-
pageInfo.Page = Math.Max(pageInfo.Page, 0);
20-
pageInfo.PageSize = Math.Max(Math.Min(pageInfo.PageSize, 100), 5);
20+
pageInfo.Page = Math.Max(pageInfo.Page, 0);
21+
pageInfo.PageSize = Math.Max(Math.Min(pageInfo.PageSize, 100), 5);
2122

22-
var entries = GameContext.Queries.ServerVariables(pageInfo.Page, pageInfo.PageSize)?.ToList();
23+
var entries = GameContext.Queries.ServerVariables(pageInfo.Page, pageInfo.PageSize)?.ToList();
2324

24-
return new DataPage<ServerVariableBase>
25+
return Ok(
26+
new DataPage<ServerVariableBase>
2527
{
26-
Total = ServerVariableBase.Lookup.Count(),
28+
Total = ServerVariableBase.Lookup.Count,
2729
Page = pageInfo.Page,
2830
PageSize = pageInfo.PageSize,
2931
Count = entries?.Count ?? 0,
3032
Values = entries,
31-
};
32-
}
33-
34-
[HttpGet("{guid:guid}")]
35-
public object GlobalVariableGet(Guid guid)
36-
{
37-
if (Guid.Empty == guid)
38-
{
39-
return BadRequest(@"Invalid global variable id.");
4033
}
34+
);
35+
}
4136

42-
var variable = GameContext.Queries.ServerVariableById(guid);
37+
[HttpGet("{variableId:guid}")]
38+
public IActionResult GlobalVariableGet(Guid variableId)
39+
{
40+
if (variableId == default)
41+
{
42+
return BadRequest($@"Variable id cannot be {variableId}");
43+
}
4344

44-
if (variable == null)
45-
{
46-
return NotFound($@"No global variable with id '{guid}'.");
47-
}
45+
var variable = GameContext.Queries.ServerVariableById(variableId);
4846

49-
return variable;
47+
// ReSharper disable once ConvertIfStatementToReturnStatement
48+
if (variable == null)
49+
{
50+
return NotFound($@"No global variable with id '{variableId}'.");
5051
}
5152

52-
[HttpGet("{guid:guid}/value")]
53-
public object GlobalVariableGetValue(Guid guid)
53+
return Ok(variable);
54+
}
55+
56+
[HttpGet("{variableId:guid}/value")]
57+
public IActionResult GlobalVariableGetValue(Guid variableId)
58+
{
59+
if (variableId == default)
5460
{
55-
if (Guid.Empty == guid)
56-
{
57-
return BadRequest(@"Invalid global variable id.");
58-
}
61+
return BadRequest($@"Variable id cannot be {variableId}");
62+
}
5963

60-
var variable = GameContext.Queries.ServerVariableById(guid);
64+
var variable = GameContext.Queries.ServerVariableById(variableId);
6165

62-
if (variable == null)
63-
{
64-
return NotFound($@"No global variable with id '{guid}'.");
65-
}
66-
67-
return new
68-
{
69-
value = variable.Value.Value,
70-
};
66+
if (variable == null)
67+
{
68+
return NotFound($@"No global variable with id '{variableId}'.");
7169
}
7270

73-
[HttpPost("{guid:guid}")]
74-
public object GlobalVariableSet(Guid guid, [FromBody] VariableValueBody valueBody)
75-
{
76-
if (Guid.Empty == guid)
71+
return Ok(
72+
new VariableValueBody
7773
{
78-
return BadRequest(@"Invalid global variable id.");
74+
Value = variable.Value.Value,
7975
}
76+
);
77+
}
8078

81-
var variable = GameContext.Queries.ServerVariableById(guid);
79+
[HttpPost("{variableId:guid}")]
80+
public IActionResult GlobalVariableSet(Guid variableId, [FromBody] VariableValueBody variableValue)
81+
{
82+
if (variableId == default)
83+
{
84+
return BadRequest($@"Variable id cannot be {variableId}");
85+
}
8286

83-
if (variable == null)
84-
{
85-
return NotFound($@"No global variable with id '{guid}'.");
86-
}
87+
var variable = GameContext.Queries.ServerVariableById(variableId);
8788

88-
var changed = true;
89-
if (variable.Value?.Value == valueBody.Value)
90-
{
91-
changed = false;
92-
}
93-
variable.Value.Value = valueBody.Value;
89+
if (variable == null)
90+
{
91+
return NotFound($@"No global variable with id '{variableId}'.");
92+
}
9493

95-
if (changed)
94+
var changed = false;
95+
if (variable.Value != null)
96+
{
97+
if (variable.Value.Value != variableValue.Value)
9698
{
97-
Player.StartCommonEventsWithTriggerForAll(Enums.CommonEventTrigger.ServerVariableChange, "", guid.ToString());
99+
variable.Value.Value = variableValue.Value;
100+
changed = true;
98101
}
99-
DbInterface.UpdatedServerVariables.AddOrUpdate(variable.Id, variable, (key, oldValue) => variable);
102+
}
100103

101-
return variable;
104+
// ReSharper disable once InvertIf
105+
if (changed)
106+
{
107+
Player.StartCommonEventsWithTriggerForAll(
108+
CommonEventTrigger.ServerVariableChange,
109+
"",
110+
variableId.ToString()
111+
);
112+
DbInterface.UpdatedServerVariables.AddOrUpdate(variable.Id, variable, (_, _) => variable);
102113
}
114+
115+
return Ok(variable);
103116
}
104-
}
117+
}

0 commit comments

Comments
 (0)