public IHttpActionResult CreateComment(Comment comment) { if (!ModelState.IsValid) { return(Content(HttpStatusCode.BadRequest, "Invalid data")); } _context.Comments.Add(comment); _context.SaveChanges(); return(Ok("Comment added")); }
public IHttpActionResult CreateStudyTopic(StudyTopic studyTopic) { if (!ModelState.IsValid) { return(Content(HttpStatusCode.BadRequest, "Invalid data")); } _context.StudyTopics.Add(studyTopic); _context.SaveChanges(); return(Ok("Study topic added")); }
public IHttpActionResult CreateRegGroup(RegGroup regGroup) { if (!ModelState.IsValid) { return(Content(HttpStatusCode.BadRequest, "Invalid data")); } _context.RegGroups.Add(regGroup); _context.SaveChanges(); return(Ok("Reg group created")); }
public ActionResult CreateCourse(TrainingCourse course) { if (!ModelState.IsValid) { return(View("NewCourse")); } _context.TrainingCourses.Add(course); _context.SaveChanges(); return(RedirectToAction("TrainingCourses", "Staff")); }
public IHttpActionResult CreateSubject(Subject data) { if (data.Name.IsNullOrWhiteSpace() || !ModelState.IsValid) { return(Content(HttpStatusCode.BadRequest, "Invalid data")); } var subjectToAdd = data; _context.Subjects.Add(subjectToAdd); _context.SaveChanges(); return(Ok("Subject created")); }
public IHttpActionResult DeleteSale(int id) { var saleInDb = _context.Sales.SingleOrDefault(p => p.Id == id); if (saleInDb == null) { return(Content(HttpStatusCode.NotFound, "Sale not found")); } _context.Sales.Remove(saleInDb); _context.SaveChanges(); return(Ok("Sale deleted")); }
public IHttpActionResult DeleteProduct(int id) { var productInDb = _context.Products.SingleOrDefault(p => p.Id == id); if (productInDb == null) { return(Content(HttpStatusCode.NotFound, "Product not found")); } _context.Products.Remove(productInDb); _context.SaveChanges(); return(Ok("Product deleted")); }
public IHttpActionResult DeleteYearGroup(int id) { var yearGroupInDb = _context.YearGroups.SingleOrDefault(x => x.Id == id); if (yearGroupInDb == null) { return(Content(HttpStatusCode.NotFound, "Year group not found")); } _context.YearGroups.Remove(yearGroupInDb); _context.SaveChanges(); return(Ok("Year group deleted")); }
public IHttpActionResult CreateCommentBank(CommentBank commentBank) { if (!ModelState.IsValid || commentBank.Name.IsNullOrWhiteSpace()) { return(Content(HttpStatusCode.BadRequest, "Invalid data")); } if (_context.CommentBanks.Any(x => x.Name == commentBank.Name)) { return(Content(HttpStatusCode.BadRequest, "Comment bank already exists")); } _context.CommentBanks.Add(commentBank); _context.SaveChanges(); return(Ok("Comment bank added")); }
public IHttpActionResult AddDocument(StudentDocumentUpload data) { var student = _context.Students.SingleOrDefault(x => x.Id == data.Student); if (student == null) { return(Content(HttpStatusCode.NotFound, "Student not found")); } var document = data.Document; var currentUserId = User.Identity.GetUserId(); var uploader = _context.Staff.Single(x => x.UserId == currentUserId); if (uploader == null) { return(Content(HttpStatusCode.BadRequest, "Uploader not found")); } document.UploaderId = uploader.Id; document.IsGeneral = false; document.Approved = true; document.Date = DateTime.Now; var isUriValid = Uri.TryCreate(document.Url, UriKind.Absolute, out var uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); if (!isUriValid) { return(Content(HttpStatusCode.BadRequest, "The URL entered is not valid")); } _context.Documents.Add(document); _context.SaveChanges(); var studentDocument = new StudentDocument { DocumentId = document.Id, StudentId = data.Student }; _context.StudentDocuments.Add(studentDocument); _context.SaveChanges(); return(Ok("Document added")); }
public IHttpActionResult AddDocument(Document document) { var IsUriValid = Uri.TryCreate(document.Url, UriKind.Absolute, out var uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); if (!IsUriValid) { return(Content(HttpStatusCode.BadRequest, "The URL entered is not valid")); } var uploader = new Staff(); var uploaderId = document.UploaderId; if (uploaderId == 0) { var userId = User.Identity.GetUserId(); uploader = _context.Staff.SingleOrDefault(x => x.UserId == userId); if (uploader == null) { return(Content(HttpStatusCode.BadRequest, "User does not have a personnel profile")); } } if (uploaderId != 0) { uploader = _context.Staff.SingleOrDefault(x => x.Id == uploaderId); } if (uploader == null) { return(Content(HttpStatusCode.NotFound, "Staff member not found")); } document.UploaderId = uploader.Id; document.IsGeneral = true; document.Date = DateTime.Now; _context.Documents.Add(document); _context.SaveChanges(); return(Ok("Document added")); }
public IHttpActionResult AddToBasket(BasketItem data) { var studentQuery = _context.Students.SingleOrDefault(x => x.Id == data.StudentId); if (studentQuery == null) { return(Content(HttpStatusCode.NotFound, "Student not found")); } var productToAdd = _context.Products.SingleOrDefault(x => x.Id == data.ProductId); if (productToAdd == null) { return(Content(HttpStatusCode.NotFound, "Product not found")); } if (!productToAdd.Visible) { return(Content(HttpStatusCode.BadRequest, "Product not available")); } var purchased = _context.Sales.Where(x => x.StudentId == data.StudentId && x.ProductId == data.ProductId && x.Product.OnceOnly); var inBasket = _context.BasketItems.Where(x => x.StudentId == data.StudentId && x.ProductId == data.ProductId && x.Product.OnceOnly); if (purchased.Any() || inBasket.Any()) { return(Content(HttpStatusCode.BadRequest, "This product cannot be purchased more than once")); } var itemToAdd = new BasketItem { ProductId = data.ProductId, StudentId = data.StudentId }; _context.BasketItems.Add(itemToAdd); _context.SaveChanges(); return(Ok("Item added to basket")); }
public IHttpActionResult DeleteCourse(int courseId) { var courseInDb = _context.TrainingCourses.Single(x => x.Id == courseId); if (courseInDb == null) { return(Content(HttpStatusCode.NotFound, "Training course not found")); } if (courseInDb.TrainingCertificates.Any()) { return(Content(HttpStatusCode.BadRequest, "Cannot delete course that has issued certificates")); } _context.TrainingCourses.Remove(courseInDb); _context.SaveChanges(); return(Ok("Training course deleted")); }
public IHttpActionResult CreateResultSet(ResultSet data) { if (data.Name.IsNullOrWhiteSpace() || !ModelState.IsValid) { return(Content(HttpStatusCode.BadRequest, "Invalid Data")); } var rsToAdd = data; var currentRsExists = _context.ResultSets.Any(x => x.IsCurrent) && _context.ResultSets.Any(); if (!currentRsExists) { rsToAdd.IsCurrent = true; } _context.ResultSets.Add(rsToAdd); _context.SaveChanges(); return(Ok("Result set created")); }
public IHttpActionResult CreateLog(Log data) { var authorId = data.AuthorId; var author = new Staff(); if (authorId == 0) { var userId = User.Identity.GetUserId(); author = _context.Staff.SingleOrDefault(x => x.UserId == userId); if (author == null) { return(Content(HttpStatusCode.BadRequest, "User does not have a personnel profile")); } } if (authorId != 0) { author = _context.Staff.SingleOrDefault(x => x.Id == authorId); } if (author == null) { return(Content(HttpStatusCode.NotFound, "Staff member not found")); } data.Date = DateTime.Now; data.AuthorId = author.Id; if (!ModelState.IsValid) { return(Content(HttpStatusCode.BadRequest, "Invalid data")); } var log = data; _context.Logs.Add(log); _context.SaveChanges(); return(Ok("Log created")); }
public IHttpActionResult CreateLessonPlan(LessonPlan plan) { if (!ModelState.IsValid) { return(Content(HttpStatusCode.BadRequest, "Invalid data")); } var authorId = plan.AuthorId; var author = new Staff(); if (authorId == 0) { var userId = User.Identity.GetUserId(); author = _context.Staff.SingleOrDefault(x => x.UserId == userId); if (author == null) { return(Content(HttpStatusCode.BadRequest, "User does not have a personnel profile")); } } if (authorId != 0) { author = _context.Staff.SingleOrDefault(x => x.Id == authorId); } if (author == null) { return(Content(HttpStatusCode.NotFound, "Staff member not found")); } plan.AuthorId = author.Id; _context.LessonPlans.Add(plan); _context.SaveChanges(); return(Ok("Lesson plan added")); }
public IHttpActionResult CreateTrainingCertificate(TrainingCertificate trainingCertificateDto) { if (!ModelState.IsValid) { return(Content(HttpStatusCode.BadRequest, "Invalid data")); } var userId = User.Identity.GetUserId(); var userPerson = _context.Staff.SingleOrDefault(x => x.UserId == userId); if (trainingCertificateDto.StaffId == userPerson.Id) { return(Content(HttpStatusCode.BadRequest, "Cannot add a certificate for yourself")); } var cert = trainingCertificateDto; _context.TrainingCertificates.Add(cert); _context.SaveChanges(); return(Ok("Certificate added")); }
public static MyPortalDbContext GetTestData() { var effortConnection = DbConnectionFactory.CreateTransient(); var context = new MyPortalDbContext(effortConnection); #region BasketItems var basketItems = new List <BasketItem> { new BasketItem { Id = 1, StudentId = 1, ProductId = 1 }, new BasketItem { Id = 2, ProductId = 1, StudentId = 1 }, new BasketItem { Id = 3, ProductId = 1, StudentId = 1 }, new BasketItem { Id = 4, ProductId = 3, StudentId = 3 } }; #endregion #region CommentBanks var commentBanks = new List <CommentBank> { new CommentBank { Name = "Opening", Id = 1 }, new CommentBank { Name = "Middle", Id = 2 }, new CommentBank { Name = "Closing", Id = 3 } }; #endregion #region Comments var comments = new List <Comment> { new Comment { CommentBankId = 1, Value = "Hello" }, new Comment { CommentBankId = 2, Value = "<he> works very hard" }, new Comment { CommentBankId = 2, Value = "<he> needs to improve his work" }, new Comment { CommentBankId = 3, Value = "Thank you" } }; #endregion #region Documents var documents = new List <Document> { new Document { Description = "Doc1", Url = "http://ftp.test.com/doc1", Date = DateTime.Today, IsGeneral = true, Approved = true, UploaderId = 1 }, new Document { Description = "Doc2", Url = "http://ftp.test.com/doc2", Date = DateTime.Today, IsGeneral = true, Approved = true, UploaderId = 1 }, new Document { Description = "Doc3", Url = "http://ftp.test.com/doc3", Date = DateTime.Today, IsGeneral = true, Approved = false, UploaderId = 1 }, new Document { Description = "Doc4", Url = "http://ftp.test.com/doc4", Date = DateTime.Today, IsGeneral = false, Approved = true, UploaderId = 1 } }; #endregion #region Grades var grades = new List <Grade>(); #endregion #region GradeSets var gradeSets = new List <GradeSet>(); #endregion #region LessonPlans var lessonPlans = new List <LessonPlan> { }; #endregion #region Logs var logs = new List <Log> { new Log { Date = DateTime.Now, AuthorId = 3, Message = "Test", StudentId = 3, TypeId = 1 }, new Log { Date = DateTime.Now, AuthorId = 3, Message = "Test2", StudentId = 3, TypeId = 2 }, new Log { Date = DateTime.Today, AuthorId = 3, Message = "Test3", StudentId = 3, TypeId = 3 }, new Log { Date = DateTime.Today, AuthorId = 3, Message = "Test4", StudentId = 3, TypeId = 4 } }; #endregion #region LogTypes var logTypes = new List <LogType> { new LogType { Name = "Type 1" }, new LogType { Name = "Type 2" }, new LogType { Name = "Type 3" }, new LogType { Name = "Type 4" } }; #endregion #region Products var products = new List <Product> { new Product { Id = 1, Description = "Art Pack", Price = (decimal)7.50, OnceOnly = false, Visible = true }, new Product { Id = 2, Description = "School Dinner", OnceOnly = false, Visible = false, Price = (decimal)1.50 }, new Product { Id = 3, Description = "School Trip", OnceOnly = true, Visible = true, Price = (decimal)100.00 }, new Product { Id = 4, Description = "Delete Me", OnceOnly = false, Visible = true, Price = 35.99m } }; #endregion #region RegGroups var regGroups = new List <RegGroup> { new RegGroup { Id = 1, Name = "1A", TutorId = 1, YearGroupId = 1 }, new RegGroup { Id = 2, Name = "4A", TutorId = 1, YearGroupId = 2 }, new RegGroup { Id = 3, Name = "7A", YearGroupId = 3, TutorId = 1 }, new RegGroup { Id = 4, Name = "11A", YearGroupId = 4, TutorId = 1 } }; #endregion #region Results var results = new List <Result> { new Result { StudentId = 1, SubjectId = 1, ResultSetId = 1, Value = "A" }, new Result { StudentId = 1, SubjectId = 2, ResultSetId = 1, Value = "C" } }; #endregion #region ResultSets var resultSets = new List <ResultSet> { new ResultSet { Id = 1, Name = "Current", IsCurrent = true }, new ResultSet { Id = 2, Name = "Old", IsCurrent = false } }; #endregion #region Sales var sales = new List <Sale>(); #endregion #region Staff var staff = new List <Staff> { new Staff { Id = 1, FirstName = "Georgia", LastName = "Alibi", Code = "GAL", Email = "*****@*****.**", JobTitle = "Test Teacher", Title = "Mrs" }, new Staff { Id = 2, FirstName = "Chloe", LastName = "Farrar", Code = "CFA", Title = "Mrs", Email = "*****@*****.**", JobTitle = "Test Teacher" }, new Staff { Id = 3, FirstName = "Lily", LastName = "Sprague", Code = "LSP", Title = "Mrs", JobTitle = "Test SLT", Email = "*****@*****.**" }, new Staff { Id = 4, FirstName = "William", LastName = "Townsend", Code = "WTO", Title = "Mr", Email = "*****@*****.**", JobTitle = "Test SLT" } }; #endregion #region StaffDocuments var staffDocuments = new List <StaffDocument>(); #endregion #region StaffObservations var staffObservations = new List <StaffObservation>(); #endregion #region Students var students = new List <Student> { new Student { Id = 1, FirstName = "Aaron", LastName = "Aardvark", YearGroupId = 3, Email = "*****@*****.**", AccountBalance = (decimal)200.00, CandidateNumber = "1234", RegGroupId = 3, Gender = "M" }, new Student { Id = 2, FirstName = "Dorothy", LastName = "Perkins", YearGroupId = 1, Email = "*****@*****.**", CandidateNumber = "5678", AccountBalance = (decimal)10.00, RegGroupId = 1, Gender = "F" }, new Student { Id = 3, FirstName = "John", LastName = "Appleseed", YearGroupId = 2, RegGroupId = 2, Email = "*****@*****.**", AccountBalance = (decimal)0.00, CandidateNumber = "7821", Gender = "X" }, new Student { Id = 4, FirstName = "Betty", LastName = "Newbie", YearGroupId = 4, RegGroupId = 4, AccountBalance = (decimal)100.00, Email = "*****@*****.**", CandidateNumber = "6452", Gender = "F" } }; #endregion #region StudentDocuments var studentDocuments = new List <StudentDocument>(); #endregion #region StudyTopics var studyTopics = new List <StudyTopic> { }; #endregion #region Subjects var subjects = new List <Subject> { new Subject { Name = "English", LeaderId = 3, Code = "En" }, new Subject { Name = "Maths", LeaderId = 3, Code = "Ma" }, new Subject { Name = "Science", LeaderId = 3, Code = "Sc" } }; #endregion #region TrainingCertificates var trainingCertificates = new List <TrainingCertificate>(); #endregion #region TrainingCourses var trainingCourses = new List <TrainingCourse>(); #endregion #region TrainingStatuses var trainingStatuses = new List <TrainingStatus>(); #endregion #region YearGroups var yearGroups = new List <YearGroup> { new YearGroup { Id = 1, Name = "Year 1", KeyStage = 1, HeadId = 3 }, new YearGroup { Id = 2, Name = "Year 4", KeyStage = 2, HeadId = 3 }, new YearGroup { Id = 3, Name = "Year 7", HeadId = 3, KeyStage = 3 }, new YearGroup { Id = 4, Name = "Year 11", HeadId = 3, KeyStage = 4 } }; #endregion context.BasketItems.AddRange(basketItems); context.CommentBanks.AddRange(commentBanks); context.Comments.AddRange(comments); context.Documents.AddRange(documents); context.Grades.AddRange(grades); context.GradeSets.AddRange(gradeSets); context.LessonPlans.AddRange(lessonPlans); context.Logs.AddRange(logs); context.LogTypes.AddRange(logTypes); context.Products.AddRange(products); context.RegGroups.AddRange(regGroups); context.Results.AddRange(results); context.ResultSets.AddRange(resultSets); context.Sales.AddRange(sales); context.Staff.AddRange(staff); context.StaffDocuments.AddRange(staffDocuments); context.StaffObservations.AddRange(staffObservations); context.Students.AddRange(students); context.StudentDocuments.AddRange(studentDocuments); context.StudyTopics.AddRange(studyTopics); context.Subjects.AddRange(subjects); context.TrainingCertificates.AddRange(trainingCertificates); context.TrainingCourses.AddRange(trainingCourses); context.TrainingStatuses.AddRange(trainingStatuses); context.YearGroups.AddRange(yearGroups); context.SaveChanges(); return(context); }
public async Task <IHttpActionResult> AttachPerson([FromBody] UserProfile data) { if (data.RoleName != "Staff" && data.RoleName != "Student") { return(Content(HttpStatusCode.BadRequest, "User can only be assigned student or staff as primary role")); } var userInDb = _identity.Users.FirstOrDefault(u => u.Id == data.UserId); var roleToAdd = _identity.Roles.FirstOrDefault(r => r.Name == data.RoleName); var userIsAttached = _context.Students.Any(x => x.UserId == data.UserId) || _context.Staff.Any(x => x.UserId == data.UserId); if (userInDb == null) { return(Content(HttpStatusCode.BadRequest, "User not found")); } if (roleToAdd == null) { return(Content(HttpStatusCode.BadRequest, "Role not found")); } if (userIsAttached) { return(Content(HttpStatusCode.BadRequest, "User is already attached to a person")); } if (data.RoleName == "Staff") { var roles = await _userManager.GetRolesAsync(data.UserId); await _userManager.RemoveFromRolesAsync(data.UserId, roles.ToArray()); await _userManager.AddToRoleAsync(data.UserId, "Staff"); var personInDb = _context.Staff.Single(x => x.Id == data.PersonId); if (personInDb.UserId != null) { return(Content(HttpStatusCode.BadRequest, "Another user is already attached to this person")); } personInDb.UserId = userInDb.Id; _context.SaveChanges(); return(Ok("User attached to person")); } if (data.RoleName == "Student") { var roles = await _userManager.GetRolesAsync(data.UserId); await _userManager.RemoveFromRolesAsync(data.UserId, roles.ToArray()); await _userManager.AddToRoleAsync(data.UserId, "Student"); var personInDb = _context.Students.Single(x => x.Id == data.PersonId); if (personInDb.UserId != null) { return(Content(HttpStatusCode.BadRequest, "Another user is already attached to this person")); } personInDb.UserId = userInDb.Id; _context.SaveChanges(); return(Ok("User attached to person")); } return(BadRequest()); }
public IHttpActionResult UploadResults(int resultSetId) { if (!File.Exists(@"C:\MyPortal\Files\Results\import.csv")) { return(Content(HttpStatusCode.NotFound, "File not found")); } var stream = new FileStream(@"C:\MyPortal\Files\Results\import.csv", FileMode.Open); var subjects = _context.Subjects.OrderBy(x => x.Name).ToList(); var numResults = 0; var resultSet = _context.ResultSets.SingleOrDefault(x => x.Id == resultSetId); if (resultSet == null) { return(Content(HttpStatusCode.NotFound, "Result set not found")); } using (var reader = new StreamReader(stream)) { reader.ReadLine(); while (!reader.EndOfStream) { var line = reader.ReadLine(); if (line == null) { continue; } var values = line.Split(','); for (var i = 0; i < subjects.Count; i++) { var studentMisId = values[4]; var student = _context.Students.SingleOrDefault(x => x.MisId == studentMisId); if (student == null) { continue; } var result = new Result { StudentId = student.Id, ResultSetId = resultSet.Id, SubjectId = subjects[i].Id, Value = values[5 + i] }; if (result.Value.Equals("")) { continue; } _context.Results.Add(result); numResults++; } } } stream.Dispose(); var guid = Guid.NewGuid(); File.Move(@"C:/MyPortal/Files/Results/import.csv", @"C:/MyPortal/Files/Results/" + guid + "_IMPORTED.csv"); _context.SaveChanges(); return(Ok(numResults + " results found and imported")); }