Startpunkten
This commit is contained in:
161
Controllers/LoginController.cs
Normal file
161
Controllers/LoginController.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using PersonSport.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authentication; //För autentisering med Claims
|
||||
using Microsoft.AspNetCore.Authentication.Cookies; //För autentisering med Claims
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Security.Cryptography;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace PersonSport.Controllers {
|
||||
|
||||
[Authorize]
|
||||
public class LoginController : Controller {
|
||||
|
||||
[AllowAnonymous]
|
||||
public IActionResult Index() {
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult ShowAdmins() {
|
||||
List<Admin> administratorer = new();
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
administratorer = db.Administratorer.ToList();
|
||||
ViewBag.antal = db.Administratorer.Count();
|
||||
}
|
||||
|
||||
return View(administratorer);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult CreateAdmin() {
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult CreateAdmin(Admin nyAdmin) {
|
||||
try {
|
||||
|
||||
nyAdmin.Password = Sha256_hash(nyAdmin.Password);
|
||||
|
||||
if (ModelState.IsValid) {
|
||||
using (var db = new IdrottContext()) {
|
||||
// Kolla om namnet är upptaget
|
||||
var antal = db.Administratorer.Where(s => s.Username == nyAdmin.Username).Count();
|
||||
if (antal == 0) {
|
||||
db.Add(nyAdmin);
|
||||
db.SaveChanges();
|
||||
}
|
||||
else {
|
||||
TempData["Meddelande"] = "Administratören finns redan. Välj ett annat namn.";
|
||||
}
|
||||
}
|
||||
return RedirectToAction(nameof(ShowAdmins));
|
||||
}
|
||||
}
|
||||
catch (Exception) {
|
||||
throw;
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
public IActionResult DeleteAdmin(int id) {
|
||||
var admin = new Admin() { AdminId = id };
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
int antalAdmins = db.Administratorer.Count();
|
||||
if (antalAdmins <= 1) {
|
||||
TempData["TooFewAdmins"] = "Det måste finnas minst 1 Administratör kvar.";
|
||||
}
|
||||
else {
|
||||
db.Attach(admin);
|
||||
db.Remove(admin);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(ShowAdmins));
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult EditAdmin(int id) {
|
||||
Admin ny = new();
|
||||
|
||||
if (ModelState.IsValid) {
|
||||
using var db = new IdrottContext(); var admin = db.Administratorer.Where(p => p.AdminId == id).FirstOrDefault();
|
||||
ny.AdminId = admin.AdminId;
|
||||
ny.Username = admin.Username;
|
||||
db.SaveChanges();
|
||||
}
|
||||
return View(ny);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult EditAdmin(Admin updated) {
|
||||
if (ModelState.IsValid) {
|
||||
using (var db = new IdrottContext()) {
|
||||
var up = db.Administratorer.Where(p => p.AdminId == updated.AdminId).FirstOrDefault();
|
||||
|
||||
up.AdminId = updated.AdminId;
|
||||
up.Username = updated.Username;
|
||||
up.Password = Sha256_hash(updated.Password);
|
||||
db.SaveChanges();
|
||||
}
|
||||
return RedirectToAction(nameof(ShowAdmins));
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> CheckLogin(Admin attKolla, string returnUrl = null) {
|
||||
|
||||
using var db = new IdrottContext(); // Kolla uppgifterna mot DB
|
||||
// Lösenordet krypteras med SHA-256
|
||||
var loginAttempt = db.Administratorer.Where(p => p.Username == attKolla.Username && p.Password == Sha256_hash(attKolla.Password)).FirstOrDefault();
|
||||
|
||||
if (loginAttempt == null) {
|
||||
// Om de inte stämmer; skicka tillbaka till Login-sidan
|
||||
TempData["msg"] = "Inloggningen inte godkänd.";
|
||||
TempData["hint"] = "Tips: Testa 'admin' och 'password'";
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
else {
|
||||
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
identity.AddClaim(new Claim(ClaimTypes.Name, attKolla.Username));
|
||||
|
||||
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
|
||||
if (returnUrl != null)
|
||||
return Redirect(returnUrl);
|
||||
else
|
||||
return RedirectToAction("Index", "Start");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Logout() {
|
||||
await HttpContext.SignOutAsync(
|
||||
CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
return RedirectToAction("Index", "Start");
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
public static String Sha256_hash(string value) {
|
||||
StringBuilder sb = new();
|
||||
|
||||
using (var hash = SHA256.Create()) {
|
||||
Encoding enc = Encoding.UTF8;
|
||||
Byte[] result = hash.ComputeHash(enc.GetBytes(value));
|
||||
|
||||
foreach (Byte b in result)
|
||||
sb.Append(b.ToString("x2"));
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
229
Controllers/PersonController.cs
Normal file
229
Controllers/PersonController.cs
Normal file
@@ -0,0 +1,229 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using PersonSport.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace PersonSport.Controllers {
|
||||
|
||||
[Authorize]
|
||||
public class PersonController : Controller {
|
||||
private const int FileNameLength = 8;
|
||||
|
||||
private readonly IWebHostEnvironment Environment;
|
||||
|
||||
public PersonController(IWebHostEnvironment _environment) {
|
||||
Environment = _environment;
|
||||
}
|
||||
|
||||
public IActionResult Index(string sortOrder) {
|
||||
List<Person> personer = new();
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
personer = sortOrder switch {
|
||||
"PersonId" => db.Personer.OrderBy(p => p.PersonId).ToList(),
|
||||
"PersonNamn" => db.Personer.OrderBy(p => p.PersonNamn).ToList(),
|
||||
"Epost" => db.Personer.OrderBy(p => p.Epost).ToList(),
|
||||
"Alder" => db.Personer.OrderBy(p => p.Alder).ToList(),
|
||||
"StartDatum" => db.Personer.OrderBy(p => p.StartDatum).ToList(),
|
||||
_ => db.Personer.OrderBy(p => p.PersonId).ToList(),
|
||||
};
|
||||
}
|
||||
|
||||
return View(personer);
|
||||
}
|
||||
|
||||
/* *********************************************************
|
||||
* Function: Search()
|
||||
* -----------------------------------------
|
||||
* Sammanfattning: Söker i databasen efter en viss post genom
|
||||
* att söka efter namn eller e-postadress.
|
||||
* Skapar en lista av personer och sparar undan söksträngen.
|
||||
* Populerar listan genom ett lambda-uttryck som motsvarade söksträngen.
|
||||
* Denna lista av personer skickas slutligen till Vyn.
|
||||
* ****************************************************** */
|
||||
[HttpPost]
|
||||
public IActionResult Search(IFormCollection fc) {
|
||||
string search = fc["search"];
|
||||
|
||||
List<Person> personer = new();
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
personer = db.Personer.Where(p => p.PersonNamn.ToLower().Contains(search.ToLower()) || p.Epost.ToLower().Contains(search.ToLower())).ToList();
|
||||
}
|
||||
|
||||
return View(personer);
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpGet]
|
||||
public IActionResult Create() {
|
||||
return View();
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpPost]
|
||||
public IActionResult Create(IFormCollection fc) {
|
||||
Person person = new();
|
||||
try {
|
||||
person.PersonNamn = fc["PersonNamn"];
|
||||
person.Epost = fc["Epost"];
|
||||
person.Visningsbild = fc["Visningsbild"];
|
||||
person.Alder = int.Parse(fc["Alder"]);
|
||||
|
||||
if (person.Visningsbild != "") {
|
||||
person.Visningsbild = Request.Form.Files[0].FileName;
|
||||
IFormFileCollection files = Request.Form.Files;
|
||||
long size = files.Sum(f => f.Length);
|
||||
string BaseURL = this.Environment.WebRootPath + @"\images\userphoto\";
|
||||
|
||||
foreach (var formFile in files) {
|
||||
string localFileName = DateTime.UtcNow.Ticks.ToString()[FileNameLength..] + ".jpg";
|
||||
var filePath = BaseURL + localFileName;
|
||||
person.Visningsbild = 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
|
||||
person.Visningsbild = "noimage.jpg";
|
||||
if (ModelState.IsValid) {
|
||||
person.StartDatum = DateTime.Now;
|
||||
using (var db = new IdrottContext()) {
|
||||
db.Add(person);
|
||||
db.SaveChanges();
|
||||
}
|
||||
TempData["MeddelandeSuccess"] = "Medlemmen är tillagd i systemet.";
|
||||
return RedirectToAction("Index", "Start");
|
||||
}
|
||||
}
|
||||
catch (Exception) {
|
||||
throw;
|
||||
}
|
||||
TempData["MeddelandeFail"] = "Kunde inte lägga till personen.";
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
|
||||
public IActionResult Delete(int id) {
|
||||
try {
|
||||
var person = new Person() { PersonId = id };
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
db.Attach(person);
|
||||
db.Remove(person);
|
||||
db.SaveChanges();
|
||||
}
|
||||
TempData["MeddelandeSuccess"] = "Medlemmen är borttagen ur systemet.";
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
catch (Exception) {
|
||||
TempData["MeddelandeFail"] = "Kunde inte ta bort personen.";
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Edit(int id) {
|
||||
Person p = new();
|
||||
try {
|
||||
using (var db = new IdrottContext()) {
|
||||
var person = db.Personer.Where(p => p.PersonId == id).FirstOrDefault();
|
||||
p.PersonId = person.PersonId;
|
||||
p.PersonNamn = person.PersonNamn;
|
||||
p.Epost = person.Epost;
|
||||
p.Alder = person.Alder;
|
||||
p.Visningsbild = person.Visningsbild;
|
||||
|
||||
db.SaveChanges();
|
||||
}
|
||||
return View(p);
|
||||
}
|
||||
catch (Exception) {
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Edit(IFormCollection fc) {
|
||||
Person person = new();
|
||||
bool bytaBild = false;
|
||||
|
||||
try {
|
||||
person.PersonId = int.Parse(fc["PersonId"]);
|
||||
person.PersonNamn = fc["PersonNamn"];
|
||||
person.Epost = fc["Epost"];
|
||||
person.Visningsbild = fc["Visningsbild"];
|
||||
person.Alder = int.Parse(fc["Alder"]);
|
||||
|
||||
if (person.Visningsbild == "")
|
||||
bytaBild = false;
|
||||
else
|
||||
bytaBild = true;
|
||||
|
||||
if (bytaBild) {
|
||||
person.Visningsbild = Request.Form.Files[0].FileName;
|
||||
IFormFileCollection files = Request.Form.Files;
|
||||
long size = files.Sum(f => f.Length);
|
||||
string BaseURL = this.Environment.WebRootPath + @"\images\userphoto\";
|
||||
|
||||
foreach (var formFile in files) {
|
||||
string localFileName = DateTime.UtcNow.Ticks.ToString()[FileNameLength..] + ".jpg";
|
||||
var filePath = BaseURL + localFileName;
|
||||
person.Visningsbild = 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
|
||||
person.Visningsbild = "noimage.jpg";
|
||||
|
||||
if (ModelState.IsValid) {
|
||||
using (var db = new IdrottContext()) {
|
||||
var up = db.Personer.Where(p => p.PersonId == person.PersonId).FirstOrDefault();
|
||||
up.PersonId = person.PersonId;
|
||||
up.PersonNamn = person.PersonNamn;
|
||||
up.Epost = person.Epost;
|
||||
up.Alder = person.Alder;
|
||||
if (bytaBild)
|
||||
up.Visningsbild = person.Visningsbild;
|
||||
|
||||
db.SaveChanges();
|
||||
}
|
||||
TempData["MeddelandeSuccess"] = "Medlemmen är editerat med de nya uppgifterna.";
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
catch (Exception) {
|
||||
throw;
|
||||
}
|
||||
TempData["MeddelandeFail"] = "Kunde inte editera personen.";
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
119
Controllers/PersonSportController.cs
Normal file
119
Controllers/PersonSportController.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using PersonSport.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace PersonSport.Controllers {
|
||||
|
||||
[Authorize]
|
||||
public class PersonSportController : Controller {
|
||||
public IActionResult Index(int? sportens_id = 1) {
|
||||
var db = new IdrottContext();
|
||||
|
||||
ViewBag.AllaSporter = db.Sporter.ToList();
|
||||
|
||||
ViewBag.sportens_id = sportens_id;
|
||||
ViewBag.SportNamn = db.Sporter
|
||||
.Where(a => a.SportId == sportens_id)
|
||||
.Select(a => a.SportNamn).Single();
|
||||
|
||||
ViewBag.PersonerIValdSport = (from pers in db.Personer
|
||||
where pers.PersonSporter.Any(c => c.SportId == sportens_id)
|
||||
select pers).ToList();
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Enroll() {
|
||||
List<Person> personer = new();
|
||||
List<Sport> sporter = new();
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
personer = db.Personer.ToList();
|
||||
}
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
sporter = db.Sporter.ToList();
|
||||
}
|
||||
|
||||
ViewBag.personer = personer;
|
||||
ViewBag.sporter = sporter;
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Enroll(IFormCollection fc) {
|
||||
try {
|
||||
int valtNamn = int.Parse(fc["Namn"]);
|
||||
int valdSport = int.Parse(fc["Sport"]);
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
PersonSport.Models.PersonSport ps = new() { PersonId = valtNamn, SportId = valdSport };
|
||||
db.Add(ps);
|
||||
db.SaveChanges();
|
||||
}
|
||||
TempData["MeddelandeSuccess"] = "Medlemmen är tillagd i sporten.";
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
catch (Exception) {
|
||||
TempData["MeddelandeFail"] = "Kunde inte lägga till medlemmen i sporten.";
|
||||
return View("/Start/Error");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public IActionResult Deroll() {
|
||||
List<Person> personer = new();
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
personer = db.Personer.ToList();
|
||||
}
|
||||
|
||||
return View(personer);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult DerollSport(IFormCollection fc) {
|
||||
try {
|
||||
int valdPersonId = int.Parse(fc["PersonId"]);
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
ViewBag.SportForValdPerson = (from sport in db.Sporter
|
||||
where sport.PersonSporter.Any(c => c.PersonId == valdPersonId)
|
||||
select sport).ToList();
|
||||
}
|
||||
ViewBag.PersonId = valdPersonId;
|
||||
return View();
|
||||
}
|
||||
catch (FormatException) {
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult DerollDone(IFormCollection fc) {
|
||||
try {
|
||||
int valtPersonId = int.Parse(fc["PersonId"]);
|
||||
int valtSportId = int.Parse(fc["SportId"]);
|
||||
|
||||
using (var db = new IdrottContext()) {
|
||||
PersonSport.Models.PersonSport ps = new() { PersonId = valtPersonId, SportId = valtSportId };
|
||||
db.Remove(ps);
|
||||
db.SaveChanges();
|
||||
}
|
||||
TempData["MeddelandeSuccess"] = "Medlemmen är borttagen från sporten.";
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
catch (FormatException) {
|
||||
TempData["MeddelandeFail"] = "Kunde inte ta bort medlemmen från sporten";
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
79
Controllers/StartController.cs
Normal file
79
Controllers/StartController.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using PersonSport.Models;
|
||||
|
||||
|
||||
namespace PersonSport.Controllers {
|
||||
public class StartController : Controller {
|
||||
|
||||
public IActionResult Index() {
|
||||
return View();
|
||||
}
|
||||
public IActionResult Priser() {
|
||||
return View();
|
||||
}
|
||||
public IActionResult Kurser() {
|
||||
return View();
|
||||
}
|
||||
public IActionResult Receptionen() {
|
||||
return View();
|
||||
}
|
||||
public IActionResult Oppettider() {
|
||||
return View();
|
||||
}
|
||||
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult SendMail(IFormCollection fc) {
|
||||
MailViewModel mvm = new();
|
||||
|
||||
if (ModelState.IsValid) {
|
||||
string formFrom = fc["Epost"];
|
||||
string formSubject = fc["Subject"];
|
||||
string formMessage = fc["Message"];
|
||||
|
||||
|
||||
string to = "christian.ohlsson@gmail.com"; // Mottagarens e-postadress
|
||||
string from = "imcoh@hv.se"; // Avsändarens e-postadress
|
||||
|
||||
string mailIntro = "<h3>Detta är ett meddelande från formuläret på SportPalatset</h3>"
|
||||
+ "Avsändare: <b>" + formFrom + "</b><br />"
|
||||
+ "Meddelandet lyder:<br /><br />";
|
||||
|
||||
string mailEnd = "<br /><br /><br /><br />"
|
||||
+ "-------------------------------------------------------------"
|
||||
+ "<p>Tänk på miljön och skriv inte ut detta mail</p>"
|
||||
+ "<img src='https://crille.org/sportpalatsetlogo.png' alt='Logo'>";
|
||||
|
||||
MailMessage message = new(from, to);
|
||||
|
||||
message.Subject = formSubject;
|
||||
message.Body = mailIntro + formMessage + mailEnd;
|
||||
message.BodyEncoding = Encoding.UTF8;
|
||||
message.IsBodyHtml = true;
|
||||
SmtpClient client = new("smtp.live.com", 587); // Outlook smtp
|
||||
NetworkCredential basicCredential1 = new("imcoh@hv.se", "s3jsVYQc3Bs2N4MN");
|
||||
client.EnableSsl = true;
|
||||
client.UseDefaultCredentials = false;
|
||||
client.Credentials = basicCredential1;
|
||||
try {
|
||||
TempData["MeddelandeSuccess"] = "Ditt meddelande har skickats till SportPalatset.";
|
||||
client.Send(message);
|
||||
}
|
||||
|
||||
catch (Exception) {
|
||||
throw;
|
||||
}
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
else {
|
||||
TempData["MeddelandeFail"] = "Mailet har inte skickats";
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user