コード例 #1
0
        private void SetUpClient()
        {
            var builder = new WebHostBuilder()
                          .UseStartup <Startup>()
                          .ConfigureServices(services =>
            {
                var context = new TestProjectContext(new DbContextOptionsBuilder <TestProjectContext>()
                                                     .UseSqlite("DataSource=:memory:")
                                                     .EnableSensitiveDataLogging()
                                                     .Options);

                services.RemoveAll(typeof(TestProjectContext));
                services.AddSingleton(context);

                context.Database.OpenConnection();
                context.Database.EnsureCreated();

                context.SaveChanges();

                // Clear local context cache
                foreach (var entity in context.ChangeTracker.Entries().ToList())
                {
                    entity.State = EntityState.Detached;
                }
            });

            _server = new TestServer(builder);

            Client = _server.CreateClient();
        }
コード例 #2
0
 // PUT: api/Shedule/5
 public void Put(Schedule value)
 {
     if (value != null && value.Id != 0)
     {
         using (var connection = ConnectToDataBase.GetConnection())
         {
             connection.Open();
             using (var context = new TestProjectContext())
             {
                 context.Schedules.Load();
                 var r = context.Schedules.Find(value.Id);
                 if (r != null)
                 {
                     r.TeacherId    = value.TeacherId;
                     r.Data         = value.Data;
                     r.DisciplineId = value.DisciplineId;
                     r.EndTime      = value.EndTime;
                     r.StartTime    = value.StartTime;
                     r.GroupId      = value.GroupId;
                     context.SaveChanges();
                 }
             }
         }
     }
 }
コード例 #3
0
 // POST: api/User
 public void Post([FromBody] User value)
 {
     using (var connection = ConnectToDataBase.GetConnection())
     {
         connection.Open();
         using (var context = new TestProjectContext())
         {
             context.Users.Load();
             if (value != null)
             {
                 context.Users.Add(value);
                 context.SaveChanges();
             }
         }
     }
 }
コード例 #4
0
 // DELETE: api/User/5
 public void Delete(int id)
 {
     using (var connection = ConnectToDataBase.GetConnection())
     {
         connection.Open();
         using (var context = new TestProjectContext())
         {
             var temp = context.Users.Find(id);
             if (temp != null)
             {
                 context.Users.Load();
                 context.Users.Remove(temp);
                 context.SaveChanges();
             }
         }
     }
 }
コード例 #5
0
ファイル: GroupController.cs プロジェクト: Spaaay/TestProject
 // PUT: api/Group/5
 public void Put(Group value)
 {
     using (var connection = ConnectToDataBase.GetConnection())
     {
         connection.Open();
         using (var context = new TestProjectContext())
         {
             context.Groups.Load();
             var r = context.Groups.Find(value.Id);
             if (r != null)
             {
                 r.GroupName = value.GroupName;
                 r.StartDate = value.StartDate;
                 r.EndDate   = value.EndDate;
                 context.SaveChanges();
             }
         }
     }
 }
コード例 #6
0
        public async Task <IActionResult> Create([Bind("BookID,Title,Isbn,Summary")] Book book, int id)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                _context.SaveChanges();
                _context.Add(new Composition {
                    AuthorID = id, BookID = book.BookID
                });
                await _context.SaveChangesAsync();

                return(RedirectToRoute(new
                {
                    controller = "Authors",
                    action = "Details",
                    id = id
                }));
            }
            return(View(book));
        }
コード例 #7
0
 // PUT: api/User/5
 public void Put(User value)
 {
     using (var connection = ConnectToDataBase.GetConnection())
     {
         connection.Open();
         using (var context = new TestProjectContext())
         {
             context.Users.Load();
             var r = context.Users.Find(value.Id);
             if (r != null)
             {
                 r.FullName = value.FullName;
                 r.Login    = value.Login;
                 r.Password = value.Password;
                 r.GroupId  = value.GroupId;
                 context.SaveChanges();
             }
         }
     }
 }
コード例 #8
0
        public JsonResult CreateContact([FromBody] Contact model)
        {
            try
            {
                TestProjectContext db = new TestProjectContext();
                var contact           = new Contact();
                contact.FirstName = model.FirstName;
                contact.LastName  = model.LastName;
                contact.Birthdate = model.Birthdate;
                contact.Nid       = model.Nid;

                db.Contact.Add(contact);
                db.SaveChanges();

                return(Json(true));
            }
            catch (Exception ex)
            {
                return(Json(ex));
            }
        }
コード例 #9
0
 // PUT: api/Discipline/5
 public void Put(Discipline value)
 {
     if (value != null && value.DisciplineId != 0)
     {
         using (var connection = ConnectToDataBase.GetConnection())
         {
             connection.Open();
             using (var context = new TestProjectContext())
             {
                 context.Disciplines.Load();
                 var r = context.Disciplines.Find(value.DisciplineId);
                 if (r != null)
                 {
                     r.DisciplineName = value.DisciplineName;
                     r.TeacherId      = value.TeacherId;
                     context.SaveChanges();
                 }
             }
         }
     }
 }
コード例 #10
0
 // PUT: api/Teacher/5
 public void Put(Teacher value)
 {
     if (value != null && value.Id != 0)
     {
         using (var connection = ConnectToDataBase.GetConnection())
         {
             connection.Open();
             using (var context = new TestProjectContext())
             {
                 context.Teachers.Load();
                 var r = context.Teachers.Find(value.Id);
                 if (r != null)
                 {
                     r.FullName = value.FullName;
                     r.Phone    = value.Phone;
                     context.SaveChanges();
                 }
             }
         }
     }
 }
コード例 #11
0
        public JsonResult UpdateContact([FromBody] Contact model)
        {
            try
            {
                TestProjectContext db = new TestProjectContext();

                var contact = db.Contact.Find(model.Id);
                if (contact != null)
                {
                    contact.FirstName = model.FirstName;
                    contact.LastName  = model.LastName;
                    contact.Nid       = model.Nid;
                    contact.Birthdate = model.Birthdate;

                    db.SaveChanges();
                }
                return(Json(contact));
            }
            catch (Exception ex)
            {
                return(Json(ex));
            }
        }
コード例 #12
0
 public void AddTransaction(TransactionData t)
 {
     _context.TransactionData.Add(t);
     _context.SaveChanges();
 }