public Student GetStudent(int id) { using (var ctx = new ArgonDbContext(_connectionStringWrapper)) { return(ctx.Students.FirstOrDefault(q => q.Id == id)); } }
public ICollection <Student> GetStudents() { using (var ctx = new ArgonDbContext(_connectionStringWrapper)) { var queryable = ctx.Students.AsQueryable(); return(queryable.ToList()); } }
public void RegisterStudent(Student student) { using (var ctx = new ArgonDbContext(_connectionStringWrapper)) { ctx.Students.Add(student); ctx.SaveChanges(); } }
public void UnregisterStudent(int id) { using (var ctx = new ArgonDbContext(_connectionStringWrapper)) { var student = GetStudent(id); if (student == null) { throw new ArgumentException($"Student {id} does not exist."); } ctx.Students.Remove(student); ctx.SaveChanges(); } }
public void Update(int id, Student student) { using (var ctx = new ArgonDbContext(_connectionStringWrapper)) { var entity = ctx.Students.FirstOrDefault(q => q.Id == id); if (entity == null) { throw new ArgumentException($"Student {id} does not exist."); } entity.Name = student.Name; entity.Surname = student.Surname; ctx.Students.Update(entity); ctx.SaveChanges(); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ArgonDbContext argonDbContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Argon API"); }); argonDbContext.Database.EnsureCreated(); }