Skip to content

Commit

Permalink
Merge pull request #81 from kodesonen/stian/#61-kursliste
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasjad authored Jan 26, 2021
2 parents 41ba7f8 + 6e8b204 commit ae35ff9
Show file tree
Hide file tree
Showing 112 changed files with 25,466 additions and 3,828 deletions.
76 changes: 57 additions & 19 deletions WebApp/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using WebApp.DbHandler.Models;
using WebApp.Models.Admin;
using WebApp.Helpers;
using System.Diagnostics;

namespace WebApp.Controllers {

Expand Down Expand Up @@ -57,7 +58,7 @@ public IActionResult NewCourse(Course model) {
}
} else {
if (ModelState.IsValid) {
CourseHandler.Update(model);
CourseHandler.UpdateCourse(model);
TempData["StatusMessage"] = StatusMessage.Success("Hurra!", "Kurset ble oppdatert");
} else {
TempData["StatusMessage"] = StatusMessage.Error("Oops!", "Noe gikk galt!");
Expand Down Expand Up @@ -111,33 +112,70 @@ public IActionResult ManageCourse(int id) {
//- Stian

/* Get all course modules */
List<Module> moduleList = CourseHandler.GetAllModulesById(id);
List<Module> moduleList = CourseHandler.GetModulesByCourseId(id);
ViewBag.CourseId = id;
return View(moduleList);
}

[HttpGet]
[Route("admin/kursbehandler/endre-modul")]
public IActionResult EditModule(int kurs, int modul) {
/* Check if the ID attributes are unset */
if (kurs == 0 || modul == 0)
return RedirectToAction("ViewCourses");

/* Check if course name is null or invalid */
var courseName = CourseHandler.GetCourseNameById(kurs);
if (courseName != null)
ViewBag.CourseName = courseName;
else
return RedirectToAction("ViewCourses");
Module model;
//Bruker denne inten for å legge til ny modul
//Den kan endres men må også endres i ManageCourse.cshtml
//Regner med vi ikke skal ha 69000 moduler
if (modul == 69420) {
var m = CourseHandler.GetModulesByCourseId(kurs);
int lastIndex = m.Count() - 1 > 0 ? m.Count() - 1 : 0;
Console.WriteLine(lastIndex);

model = new Module() {
CourseId = kurs,
Chapter = lastIndex > 0 ? m[lastIndex].Chapter : 1,
SubChapter = lastIndex > 0 ? m[lastIndex].SubChapter + 1 : 1,
update = false
};
return View(model);
} else {
model = CourseHandler.GetModuleById(modul);
}

/* Check if module name is null or invalid */
var moduleName = CourseHandler.GetModuleNameById(modul);
if (moduleName != null)
ViewBag.ModuleName = moduleName;
else
return RedirectToAction("ViewCourses");
model.update = true;
return View(model);
}

return View();
[HttpPost]
[Route("admin/kursbehandler/endre-modul")]
public IActionResult EditModule(Module m) {
//Oppdater tid
Console.WriteLine(m.update);
if (m.update) {
DateTime _date = DateTime.Now;
var _dateString = _date.ToString("dd-MM-yyyy");
DateTime d;
if (DateTime.TryParseExact(_dateString, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out d)) {
m.Updated = d;
}
var updateResult = CourseHandler.UpdateModule(m);
if (updateResult) {
Module module = CourseHandler.GetModulesByCourseId(m.Id).FirstOrDefault();
TempData["Message"] = "Update successful";
m.update = true;
return RedirectToAction("ManageCourse", new { id = m.CourseId });
}
TempData["Message"] = "Update failed";
} else {
//Legg inn published dato
DateTime _date = DateTime.Now;
var _dateString = _date.ToString("dd-MM-yyyy");
DateTime d;
if (DateTime.TryParseExact(_dateString, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out d)) {
m.Published = d;
}
CourseHandler.AddNewModule(m);
}
m.update = true;
return RedirectToAction("ManageCourse", new { id = m.CourseId });
}
}
}
6 changes: 3 additions & 3 deletions WebApp/DbHandler/DataSeeding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public static void Seed(this ModelBuilder modelBuilder)

// Modules
modelBuilder.Entity<Module>().HasData(
new Module { Id = 1, Name = "Test module", CourseId = 1, Chapter = 1, SubChapter = 1 },
new Module { Id = 2, Name = "Another module", CourseId = 1, Chapter = 1, SubChapter = 2 }
new Module { Id = 1, ModuleName = "Test module", CourseId = 1, Chapter = 1, SubChapter = 1 },
new Module { Id = 2, ModuleName = "Another module", CourseId = 1, Chapter = 1, SubChapter = 2 }
);
}
}
}
}
36 changes: 31 additions & 5 deletions WebApp/DbHandler/Interfaces/ICourseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ public interface ICourseHandler {

public List<Module> GetAllModules();

public List<Module> GetAllModulesById(int courseId);
public Module GetModuleById(int moduleId);

public List<Module> GetModulesByCourseId(int courseId);

public bool AddNewCourse(Course model);

public bool AddNewModule(Module model);

public string GetCourseNameById(int courseId);

public string GetModuleNameById(int moduleId);

public Course GetCourseById(int courseId);

public bool Update(Course model);
public bool UpdateCourse(Course model);

public bool UpdateModule(Module model);
}

public class CourseHandler : ICourseHandler {
Expand All @@ -45,7 +51,7 @@ public List<Module> GetAllModules() {
return allModules;
}

public List<Module> GetAllModulesById(int courseId) {
public List<Module> GetModulesByCourseId(int courseId) {
var Data = db.Modules.Where(x => x.CourseId == courseId);
List<Module> allModules = Data.ToList();
return allModules;
Expand All @@ -67,6 +73,13 @@ public bool AddNewCourse(Course model) {
return true;
}

public bool AddNewModule(Module model) {
db.Modules.Add(model);
db.SaveChanges();

return true;
}

public string GetCourseNameById(int courseId) {
var Data = db.Courses.Where(x => x.Id == courseId).FirstOrDefault();
if (Data != null)
Expand All @@ -77,7 +90,7 @@ public string GetCourseNameById(int courseId) {
public string GetModuleNameById(int moduleId) {
var Data = db.Modules.Where(x => x.Id == moduleId).FirstOrDefault();
if (Data != null)
return Data.Name;
return Data.ModuleName;
return null;
}

Expand All @@ -90,11 +103,24 @@ public Course GetCourseById(int courseId) {
return course.First();
}

public bool Update(Course model) {
public bool UpdateCourse(Course model) {
db.Courses.Update(model);
var result = db.SaveChanges();
//Returns true if more than 0 rows affected
return result > 0;
}

public bool UpdateModule(Module model) {
db.Modules.Update(model);
var result = db.SaveChanges();
//Returns true if more than 0 rows affected
return result > 0;
}

public Module GetModuleById(int moduleId) {
var Data = db.Modules.Where(x => x.Id == moduleId);
Module allModules = Data.FirstOrDefault();
return allModules;
}
}
}
Loading

0 comments on commit ae35ff9

Please sign in to comment.