Files
marechai/cicm_web/Areas/Admin/Controllers/MachinePhotosController.cs

156 lines
6.4 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading.Tasks;
using Cicm.Database.Models;
using cicm_web.Areas.Admin.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2019-05-27 19:38:17 +01:00
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
namespace cicm_web.Areas.Admin.Controllers
{
[Area("Admin")]
[Authorize]
public class MachinePhotosController : Controller
{
readonly cicmContext _context;
public MachinePhotosController(cicmContext context)
{
_context = context;
}
// GET: MachinePhotos
public async Task<IActionResult> Index()
{
return View(await _context.MachinePhotos.Include(m => m.Machine).Include(m => m.Machine.Company)
.Include(m => m.User).Select(p => new MachinePhotoViewModel
{
Id = p.Id,
Author = p.Author,
2019-05-27 19:38:17 +01:00
License = p.License.Name,
Machine =
$"{p.Machine.Company.Name} {p.Machine.Name}",
UploadDate = p.UploadDate,
2019-05-27 19:38:17 +01:00
UploadUser = p.User.UserName,
LicenseId = p.License.Id
}).OrderBy(p => p.Machine)
.ThenBy(p => p.UploadUser).ThenBy(p => p.UploadDate).ToListAsync());
}
// GET: MachinePhotos/Details/5
public async Task<IActionResult> Details(Guid? id)
{
if(id == null) return NotFound();
MachinePhoto machinePhoto = await _context.MachinePhotos.FirstOrDefaultAsync(m => m.Id == id);
if(machinePhoto == null) return NotFound();
return View(machinePhoto);
}
// GET: MachinePhotos/Create
2019-05-27 19:38:17 +01:00
public IActionResult Create()
{
ViewData["MachineId"] =
new
SelectList(_context.Machines.OrderBy(c => c.Company.Name).ThenBy(c => c.Name).Select(m => new {m.Id, Name = $"{m.Company.Name} {m.Name}"}),
"Id", "Name");
ViewData["LicenseId"] =
new SelectList(_context.Licenses.OrderBy(c => c.Name).Select(l => new {l.Id, l.Name}), "Id", "Name");
return View();
}
// POST: MachinePhotos/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
2019-05-27 19:38:17 +01:00
// [HttpPost]
// [ValidateAntiForgeryToken]
// public async Task<IActionResult> Create(
// [Bind(
// "Author,CameraManufacturer,CameraModel,ColorSpace,Comments,Contrast,CreationDate,DigitalZoomRatio,ExifVersion,Exposure,ExposureMethod,ExposureProgram,Flash,Focal,FocalLength,FocalLengthEquivalent,HorizontalResolution,IsoRating,Lens,License,LightSource,MeteringMode,Orientation,PixelComposition,Saturation,SceneCaptureType,SceneControl,SensingMethod,Sharpness,SoftwareUsed,SubjectDistanceRange,UploadDate,VerticalResolution,WhiteBalance,Id")]
// MachinePhoto machinePhoto)
// {
// if(ModelState.IsValid)
// {
// machinePhoto.Id = Guid.NewGuid();
// _context.Add(machinePhoto);
// await _context.SaveChangesAsync();
// return RedirectToAction(nameof(Index));
// }
//
// return View(machinePhoto);
// }
// GET: MachinePhotos/Edit/5
public async Task<IActionResult> Edit(Guid? id)
{
if(id == null) return NotFound();
MachinePhoto machinePhoto = await _context.MachinePhotos.FindAsync(id);
if(machinePhoto == null) return NotFound();
return View(machinePhoto);
}
// POST: MachinePhotos/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind(
"Author,CameraManufacturer,CameraModel,ColorSpace,Comments,Contrast,CreationDate,DigitalZoomRatio,ExifVersion,Exposure,ExposureMethod,ExposureProgram,Flash,Focal,FocalLength,FocalLengthEquivalent,HorizontalResolution,IsoRating,Lens,License,LightSource,MeteringMode,Orientation,PixelComposition,Saturation,SceneCaptureType,SceneControl,SensingMethod,Sharpness,SoftwareUsed,SubjectDistanceRange,UploadDate,VerticalResolution,WhiteBalance,Id")]
MachinePhoto machinePhoto)
{
if(id != machinePhoto.Id) return NotFound();
if(ModelState.IsValid)
{
try
{
_context.Update(machinePhoto);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!MachinePhotoExists(machinePhoto.Id)) return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
return View(machinePhoto);
}
// GET: MachinePhotos/Delete/5
public async Task<IActionResult> Delete(Guid? id)
{
if(id == null) return NotFound();
MachinePhoto machinePhoto = await _context.MachinePhotos.FirstOrDefaultAsync(m => m.Id == id);
if(machinePhoto == null) return NotFound();
return View(machinePhoto);
}
// POST: MachinePhotos/Delete/5
[HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(Guid id)
{
MachinePhoto machinePhoto = await _context.MachinePhotos.FindAsync(id);
_context.MachinePhotos.Remove(machinePhoto);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool MachinePhotoExists(Guid id)
{
return _context.MachinePhotos.Any(e => e.Id == id);
}
}
}