blob: 5ef87ea3293a08826348a56f79d3ea8e87308547 (
plain) (
blame)
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
105
106
|
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using VSH.Data;
using VSH.Data.Database;
using VSH.Data.Enums;
using VSH.Data.Results;
using IOL.Helpers;
namespace VSH.Controllers;
public class CategoriesController : MainControllerBase
{
private readonly MainDbContext _context;
private readonly IStringLocalizer<SharedControllerResources> _localizer;
public CategoriesController(MainDbContext context, IStringLocalizer<SharedControllerResources> localizer) {
_context = context;
_localizer = localizer;
}
[HttpGet]
public ActionResult GetCategories() {
return Ok(_context.Categories.OrderBy(c => c.Created));
}
[HttpGet("with-products")]
public ActionResult GetCategoriesWithProducts() {
return Ok(_context.Categories.Include(c => c.Products)
.OrderBy(c => c.Created));
}
[HttpGet("{id}")]
public ActionResult GetCategory(Guid id) {
return Ok(_context.Categories.SingleOrDefault(c => c.Id == id));
}
[HttpGet("{id}/enable")]
public ActionResult EnableCategory(Guid id) {
var category = _context.Categories.SingleOrDefault(c => c.Id == id);
if (category == default)
return NotFound(new ErrorResult(_localizer["Kunne ikke finne kategorien"]));
category.VisibilityState = CategoryVisibility.DEFAULT;
category.Update();
_context.SaveChanges();
return Ok();
}
[HttpGet("{id}/disable")]
public ActionResult DisableCategory(Guid id) {
var category = _context.Categories.SingleOrDefault(c => c.Id == id);
if (category == default)
return NotFound(new ErrorResult(_localizer["Kunne ikke finne kategorien"]));
category.VisibilityState = CategoryVisibility.DISABLED;
category.Update();
_context.SaveChanges();
return Ok();
}
[HttpGet("create")]
public ActionResult CreateCategoryAsync(string name) {
if (name.IsNullOrWhiteSpace())
return BadRequest(new ErrorResult(_localizer["Ugyldig skjema"], _localizer["Navn er påkrevd"]));
if (_context.Categories.Any(c => c.Name == name))
return BadRequest(new ErrorResult(_localizer["Ugyldig skjema"],
_localizer["En kategori med det navnet finnes allerede"]));
var newCategory = new Category(name);
newCategory.SetBaseValues();
_context.Categories.Add(newCategory);
_context.SaveChanges();
return Ok(newCategory);
}
[HttpGet("{id}/update")]
public ActionResult UpdateCategoryAsync(Guid id, string newName) {
var category = _context.Categories.SingleOrDefault(c => c.Id == id);
if (category == default)
return NotFound(new ErrorResult(_localizer["Kunne ikke finne kategorien"]));
if (newName.IsNullOrWhiteSpace())
return BadRequest(new ErrorResult(_localizer["Ugyldig skjema"],
_localizer["Det nye navnet kan ikke være tomt"]));
category.Update(new Category(newName));
_context.SaveChanges();
return Ok();
}
[HttpDelete("{id}/delete")]
public ActionResult DeleteCategoryAsync(Guid id) {
var category = _context.Categories.Include(c => c.Products).SingleOrDefault(c => c.Id == id);
if (category == default)
return NotFound(new ErrorResult(_localizer["Kunne ikke finne kategorien"]));
if (category.Products.Any()) {
category.VisibilityState = CategoryVisibility.DELETED;
_context.SaveChanges();
} else {
_context.Categories.Remove(category);
_context.SaveChanges();
}
return Ok();
}
}
|