public void TestModel(int hid) { var ctgy = new LearnCategory(); Assert.False(ctgy.IsValid(null)); // Name ctgy.Name = "Test"; //ctgy.HomeID = hid; Assert.True(ctgy.IsValid(null)); // Parent ID ctgy.ParentID = 999; Assert.False(ctgy.IsValid(null)); ctgy.HomeID = hid; Assert.True(ctgy.IsValid(null)); var dbcontext = this.fixture.GetCurrentDataContext(); // Initialize data if (hid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(dbcontext); } else if (hid == DataSetupUtility.Home2ID) { fixture.InitHome2TestData(dbcontext); } Assert.False(ctgy.IsValid(dbcontext)); ctgy.ParentID = null; Assert.True(ctgy.IsValid(dbcontext)); ctgy.HomeID = 999; Assert.False(ctgy.IsValid(dbcontext)); }
public async Task <IActionResult> Post([FromBody] LearnCategory ctgy) { if (!ModelState.IsValid) { HIHAPIUtility.HandleModalStateError(ModelState); } // Check if (!ctgy.IsValid(this._context) || !ctgy.HomeID.HasValue) { throw new BadRequestException("Inputted Object IsValid Failed"); } // User String usrName = String.Empty; try { usrName = HIHAPIUtility.GetUserID(this); if (String.IsNullOrEmpty(usrName)) { throw new UnauthorizedAccessException(); } } catch { throw new UnauthorizedAccessException(); } // Check whether User assigned with specified Home ID var hms = _context.HomeMembers.Where(p => p.HomeID == ctgy.HomeID.Value && p.User == usrName).Count(); if (hms <= 0) { throw new UnauthorizedAccessException(); } ctgy.CreatedAt = DateTime.Now; ctgy.Createdby = usrName; _context.LearnCategories.Add(ctgy); await _context.SaveChangesAsync(); return(Created(ctgy)); }
public async Task TestController(int hid, string user) { var context = this.fixture.GetCurrentDataContext(); var control = new LearnCategoriesController(context); var userclaim = DataSetupUtility.GetClaimForUser(user); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = userclaim } }; // 0. Initialize data if (hid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(context); } else if (hid == DataSetupUtility.Home2ID) { fixture.InitHome2TestData(context); } var existamt = context.LearnCategories.Where(p => p.HomeID == hid || p.HomeID == null).Count(); // 1. Read all categories var items = control.Get(); var itemcnt = items.Count(); Assert.Equal(existamt, itemcnt); // 2. Insert new category var ctgy = new LearnCategory() { HomeID = hid, Name = "Test 1_UT_" + hid.ToString(), Comment = "Test 1" }; var rst1 = await control.Post(ctgy); Assert.NotNull(rst1); var rst2 = Assert.IsType <CreatedODataResult <LearnCategory> >(rst1); Assert.Equal(ctgy.Name, rst2.Entity.Name); var firstctg = rst2.Entity.ID; Assert.True(firstctg > 0); ctgiesCreated.Add(firstctg); Assert.Equal(ctgy.Comment, rst2.Entity.Comment); // 3. Read all categories items = control.Get(); itemcnt = items.Count(); Assert.Equal(existamt + 1, itemcnt); // 4. Change the category's name ctgy.Name = "Test 2"; rst1 = await control.Put(firstctg, ctgy); var rst3 = Assert.IsType <UpdatedODataResult <LearnCategory> >(rst1); Assert.Equal(ctgy.Name, rst3.Entity.Name); // 5. Delete it var rst4 = await control.Delete(firstctg); Assert.NotNull(rst4); var rst6 = Assert.IsType <StatusCodeResult>(rst4); Assert.Equal(204, rst6.StatusCode); ctgiesCreated.Clear(); // 6. Read all categories again items = control.Get(); itemcnt = items.Count(); Assert.Equal(existamt, itemcnt); await context.DisposeAsync(); }
public async Task <IActionResult> Put([FromODataUri] int key, [FromBody] LearnCategory update) { if (!ModelState.IsValid) { HIHAPIUtility.HandleModalStateError(ModelState); } if (key != update.ID) { throw new BadRequestException("Inputted ID mismatched"); } // User String usrName = String.Empty; try { usrName = HIHAPIUtility.GetUserID(this); if (String.IsNullOrEmpty(usrName)) { throw new UnauthorizedAccessException(); } } catch { throw new UnauthorizedAccessException(); } // Check whether User assigned with specified Home ID var hms = _context.HomeMembers.Where(p => p.HomeID == update.HomeID && p.User == usrName).Count(); if (hms <= 0) { throw new UnauthorizedAccessException(); } if (!update.IsValid(this._context)) { return(BadRequest()); } update.UpdatedAt = DateTime.Now; update.Updatedby = usrName; _context.Entry(update).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException exp) { if (!_context.LearnCategories.Any(p => p.ID == key)) { throw new NotFoundException("Inputted ID not found"); } else { throw new DBOperationException(exp.Message); } } return(Updated(update)); }
internal void AddMethod(LearnCategory method) { _methods.Add(new LearnMethod(method)); }
public LearnMethod(LearnCategory method) { Method = method; }