summaryrefslogtreecommitdiff
path: root/Controllers/FigureController.cs
blob: 5785f86a21a5fa333d430ec16a5e7e031c769fd6 (plain)
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
using System.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyDarling.Models;

namespace MyDarling.Controllers
{
	public class FigureController : Controller
	{
		private DataContext context;
		public FigureController(DataContext context)
		{
			this.context = context;
		}

		public async Task<IActionResult> Details(int id)
		{
			return View(await context.Figures.Where(f => f.Id == id).FirstOrDefaultAsync());
		}

		[HttpPost]
		public async Task<IActionResult> Edit(int? id)
		{
			if (id == null)
			{
				return NotFound();
			}

			var figure = await context.Figures.FindAsync(id);

			if (figure == null)
			{
				return NotFound();
			}

			var bundle = await context.UnderwearBundles
				.Where(b => b.Figures.Contains(figure))
				.FirstOrDefaultAsync();

			if (await TryUpdateModelAsync<Figure>(
				figure,
				"",
				f => f.Description))
			{
				try
				{
					await context.SaveChangesAsync();
					return RedirectToAction("Details", "Bundle", new { Id = bundle?.Id});
				}
				catch (SystemException)
				{
					ModelState.AddModelError("", "Unable to save changes");
				}
			}
			return View(figure);
		}
	}
}