Startpunkten
This commit is contained in:
236
Controllers/SportController.cs
Normal file
236
Controllers/SportController.cs
Normal file
@@ -0,0 +1,236 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using PersonSport.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace PersonSport.Controllers {
|
||||
[Authorize]
|
||||
public class SportController : Controller {
|
||||
private const int FileNameLength = 8;
|
||||
|
||||
private readonly IWebHostEnvironment Environment;
|
||||
|
||||
public SportController(IWebHostEnvironment _environment) {
|
||||
Environment = _environment;
|
||||
}
|
||||
|
||||
|
||||
public IActionResult Index() {
|
||||
List<Sport> sporter = new();
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
sporter = db.Sporter.ToList();
|
||||
}
|
||||
|
||||
return View(sporter);
|
||||
}
|
||||
|
||||
|
||||
[AllowAnonymous]
|
||||
public IActionResult Utbud() {
|
||||
List<Sport> sporter = new();
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
sporter = db.Sporter.ToList();
|
||||
}
|
||||
|
||||
return View(sporter);
|
||||
}
|
||||
|
||||
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Create() {
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Create(IFormCollection fc) {
|
||||
Sport sport = new();
|
||||
try {
|
||||
sport.SportNamn = fc["SportNamn"];
|
||||
sport.Ingress = fc["Ingress"];
|
||||
sport.DetaljText = fc["DetaljText"];
|
||||
sport.Traningstider = fc["Traningstider"];
|
||||
sport.Bakgrundsbild = fc["Bakgrundsbild"];
|
||||
|
||||
if (sport.Bakgrundsbild != "") {
|
||||
sport.Bakgrundsbild = Request.Form.Files[0].FileName;
|
||||
IFormFileCollection files = Request.Form.Files;
|
||||
long size = files.Sum(f => f.Length);
|
||||
string BaseURL = this.Environment.WebRootPath + @"\images\sport\";
|
||||
|
||||
foreach (var formFile in files) {
|
||||
string localFileName = DateTime.UtcNow.Ticks.ToString()[FileNameLength..] + ".jpg";
|
||||
var filePath = BaseURL + localFileName;
|
||||
sport.Bakgrundsbild = localFileName;
|
||||
|
||||
// Kontrollerar så att den valda filen är en bildfil
|
||||
if (formFile.ContentType == "image/jpeg" || formFile.ContentType == "image/jpg" || formFile.ContentType == "image/png" || formFile.ContentType == "image/gif") {
|
||||
if (formFile.Length > 0) {
|
||||
using var stream = new FileStream(filePath, FileMode.Create); formFile.CopyTo(stream);
|
||||
}
|
||||
else
|
||||
ViewBag.e = "Där där bilden är 0 bytes. Måste vara lite tråkig eller?";
|
||||
}
|
||||
else
|
||||
ViewBag.e = "Det där var ingen bildfil";
|
||||
}
|
||||
}
|
||||
else
|
||||
sport.Bakgrundsbild = "noimage.jpg";
|
||||
if (ModelState.IsValid) {
|
||||
using (var db = new IdrottContext()) {
|
||||
db.Add(sport);
|
||||
db.SaveChanges();
|
||||
}
|
||||
TempData["MeddelandeSuccess"] = "Sporten är tillagd i systemet.";
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
catch (Exception) {
|
||||
throw;
|
||||
}
|
||||
TempData["MeddelandeFail"] = "Kunde inte lägga till sporten.";
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
public IActionResult Delete(int id) {
|
||||
try {
|
||||
var sport = new Sport() { SportId = id };
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
db.Attach(sport);
|
||||
db.Remove(sport);
|
||||
db.SaveChanges();
|
||||
}
|
||||
TempData["MeddelandeSuccess"] = "Sporten är borttagen ur systemet.";
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
catch (Exception) {
|
||||
TempData["MeddelandeFail"] = "Kunde inte ta bort sporten.";
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Edit(int id) {
|
||||
Sport p = new();
|
||||
try {
|
||||
using (var db = new IdrottContext()) {
|
||||
var sport = db.Sporter.Where(p => p.SportId == id).FirstOrDefault();
|
||||
p.SportId = sport.SportId;
|
||||
p.SportNamn = sport.SportNamn;
|
||||
p.Ingress = sport.Ingress;
|
||||
p.DetaljText = sport.DetaljText;
|
||||
p.Traningstider = sport.Traningstider;
|
||||
p.Bakgrundsbild = sport.Bakgrundsbild;
|
||||
db.SaveChanges();
|
||||
}
|
||||
return View(p);
|
||||
}
|
||||
catch (Exception) {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Edit(IFormCollection fc) {
|
||||
Sport sport = new();
|
||||
bool bytaBild = false;
|
||||
|
||||
try {
|
||||
sport.SportId = int.Parse(fc["SportId"]);
|
||||
sport.SportNamn = fc["SportNamn"];
|
||||
sport.Ingress = fc["Ingress"];
|
||||
sport.DetaljText = fc["DetaljText"];
|
||||
sport.Traningstider = fc["Traningstider"];
|
||||
sport.Bakgrundsbild = fc["Bakgrundsbild"];
|
||||
|
||||
if (sport.Bakgrundsbild == "")
|
||||
bytaBild = false;
|
||||
else
|
||||
bytaBild = true;
|
||||
|
||||
if (bytaBild) {
|
||||
sport.Bakgrundsbild = Request.Form.Files[0].FileName;
|
||||
IFormFileCollection files = Request.Form.Files;
|
||||
long size = files.Sum(f => f.Length);
|
||||
string BaseURL = this.Environment.WebRootPath + @"\images\sport\";
|
||||
|
||||
foreach (var formFile in files) {
|
||||
string localFileName = DateTime.UtcNow.Ticks.ToString()[FileNameLength..] + ".jpg";
|
||||
var filePath = BaseURL + localFileName;
|
||||
sport.Bakgrundsbild = localFileName;
|
||||
|
||||
// Kontrollerar så att den valda filen är en bildfil
|
||||
if (formFile.ContentType == "image/jpeg" || formFile.ContentType == "image/jpg" || formFile.ContentType == "image/png" || formFile.ContentType == "image/gif") {
|
||||
if (formFile.Length > 0) {
|
||||
using var stream = new FileStream(filePath, FileMode.Create); formFile.CopyTo(stream);
|
||||
}
|
||||
else
|
||||
ViewBag.e = "Där där bilden är 0 bytes. Måste vara lite tråkig eller?";
|
||||
}
|
||||
else
|
||||
ViewBag.e = "Det där var ingen bildfil";
|
||||
}
|
||||
}
|
||||
else
|
||||
sport.Bakgrundsbild = "nosport.jpg";
|
||||
|
||||
if (ModelState.IsValid) {
|
||||
using (var db = new IdrottContext()) {
|
||||
var up = db.Sporter.Where(p => p.SportId == sport.SportId).FirstOrDefault();
|
||||
|
||||
up.SportId = sport.SportId;
|
||||
up.SportNamn = sport.SportNamn;
|
||||
up.Ingress = sport.Ingress;
|
||||
up.DetaljText = sport.DetaljText;
|
||||
up.Traningstider = sport.Traningstider;
|
||||
|
||||
if (bytaBild)
|
||||
up.Bakgrundsbild = sport.Bakgrundsbild;
|
||||
|
||||
db.SaveChanges();
|
||||
}
|
||||
TempData["MeddelandeSuccess"] = "Sporten är uppdaterad med de nya uppgifterna.";
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
catch (Exception) {
|
||||
throw;
|
||||
}
|
||||
TempData["MeddelandeFail"] = "Kunde inte editera sporten.";
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpGet]
|
||||
public IActionResult Details(int id) {
|
||||
Sport s = new();
|
||||
try {
|
||||
using (var db = new IdrottContext()) {
|
||||
var valdSport = db.Sporter.Where(p => p.SportId == id).FirstOrDefault();
|
||||
s.SportId = valdSport.SportId;
|
||||
s.SportNamn = valdSport.SportNamn;
|
||||
s.Ingress = valdSport.Ingress;
|
||||
s.DetaljText = valdSport.DetaljText;
|
||||
s.Traningstider = valdSport.Traningstider;
|
||||
s.Bakgrundsbild = valdSport.Bakgrundsbild;
|
||||
}
|
||||
ViewBag.metaAbstract = s.Ingress;
|
||||
ViewBag.metaImage = "https://sportpalatset.se/images/sport/" + s.Bakgrundsbild;
|
||||
return View(s);
|
||||
}
|
||||
catch (Exception) {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user