summaryrefslogtreecommitdiff
path: root/Pages/Figure.cshtml
blob: 76c2216dfba6125a2ed01bece5b3f9b16236892c (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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@* @page "{id}"
@model FigureModel;
@using MyDarling.Models;
@using Microsoft.EntityFrameworkCore;
@using Microsoft.AspNetCore.Mvc.RazorPages

<!DOCTYPE html>
<html>

<head>
    <title>Редактирование фотографии</title>
    <link href="/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>

<body>
    <nav class="navbar navbar-dark bg-primary">
        <a class="navbar-brand mx-4" href="/Bundle"><img height="30" src="/assets/img/logo.svg"></a>
        <a href="/Account/logout"><button class="btn btn-outline-light mx-4">Выйти</button></a>
    </nav>
    <div class="container">
        <div class="row row-cols-xl-3 row-cols-md-2 justify-content-center mt-3">
            <div class="col">
                <div class="thumbnail">
                    <a href="@Model.FilePath">
                        <img src="@Model.FilePath" class="img-thumbnail img-fluid" alt="@Model.Figure?.Description">
                    </a>
                </div>
                <form method="post" class="m-2">
                    @Html.AntiForgeryToken()
                    <div class="form-group">
                        <label class="form-label">Описание:</label>
                        <input name="description" class="form-control" value="@Model.Figure?.Description" />
                    </div>
                    <button type="submit" class="btn btn-outline-success mt-2">Сохранить</button>
                </form>
                <form asp-page-handler="Delete" method="post">
                    <button type="submit"  class="btn btn-outline-danger mt-2">Удалить</button>
                </form>
            </div>
        </div>
    </div>
</body>

</html>

@functions
{
    public class FigureModel : PageModel
    {
        private DataContext context;
        private IWebHostEnvironment environment;
        public string? FilePath { get; set; }
        public Figure? Figure { get; set; }
        public UnderwearBundle? Bundle { get; set; }
        public FigureModel(DataContext context, IWebHostEnvironment environment)
        {
            this.context = context;
            this.environment = environment;
        }

        public async Task<IActionResult> OnGetAsync(string id)
        {
            Figure = await context.Figures
            .Where(f => f.Id.Equals(id))
            .FirstOrDefaultAsync();
            if (Figure == null)
            {
                return NotFound();
            }
            Bundle = await context.UnderwearBundles
            .Where(b => b.Figures.Contains(Figure))
            .FirstOrDefaultAsync();
            if (Bundle == null)
            {
                return NotFound();
            }
            FilePath = $"/Content/{Bundle.Id}/{Figure.Id}.jpg";

            return Page();
        }
        public async Task<IActionResult> OnPostAsync(string id, string description)
        {
            Figure = await context.Figures
            .Where(f => f.Id.Equals(id))
            .FirstOrDefaultAsync();
            if (Figure != null)
            {
                Figure.Description = description ?? string.Empty;
            }
            await context.SaveChangesAsync();
            return RedirectToPage();
        }

        public async Task<IActionResult> OnPostDeleteAsync(string id)
        {
            Figure = await context.Figures.FindAsync(id);
            if (Figure == null)
            {
                return NotFound();
            }
            var parentBundle = await context.UnderwearBundles
            .Where(b => b.Figures.Contains(Figure))
            .FirstAsync();

            try
            {
                string filePath = $"/Content/{parentBundle.Id}/{Figure}.jpg";
                FileInfo figureFile = new FileInfo(environment.WebRootPath + filePath);
                if (figureFile.Exists)
                {
                    figureFile.Delete();
                }

                context.Figures.Remove(Figure);
                await context.SaveChangesAsync();
                return RedirectToPage($"/Bundle/{Bundle.Id}");
            }
            catch (DbUpdateException)
            {
                return RedirectToPage($"/Bundle/{Bundle.Id}");
            }
        }
    }
} *@