public async Task <IActionResult> Post([FromBody] FinanceAccountCategory 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(); } if (!ctgy.IsValid(this._context)) { return(BadRequest()); } ctgy.Createdby = usrName; ctgy.CreatedAt = DateTime.Now; _context.FinAccountCategories.Add(ctgy); await _context.SaveChangesAsync(); return(Created(ctgy)); }
public async Task TestCase1(int hid, string user) { var ctgyCount = DataSetupUtility.FinanceAccountCategories.Count(); var context = this.fixture.GetCurrentDataContext(); FinanceAccountCategoriesController control = new FinanceAccountCategoriesController(context); var userclaim = DataSetupUtility.GetClaimForUser(user); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = userclaim } }; // 1. Read all categories var items = control.Get(); var itemcnt = items.Count(); Assert.Equal(ctgyCount, itemcnt); // 2. Insert new category var ctgy = new FinanceAccountCategory() { 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 <FinanceAccountCategory> >(rst1); Assert.Equal(ctgy.Name, rst2.Entity.Name); var firstctg = rst2.Entity.ID; Assert.True(firstctg > 0); Assert.Equal(ctgy.Comment, rst2.Entity.Comment); // 3. Read all categories items = control.Get(); itemcnt = items.Count(); Assert.Equal(ctgyCount + 1, itemcnt); // 4. Change the category's name ctgy.Name = "Test 2"; rst1 = await control.Put(firstctg, ctgy); var rst3 = Assert.IsType <UpdatedODataResult <FinanceAccountCategory> >(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); // 6. Read all categories again items = control.Get(); itemcnt = items.Count(); Assert.Equal(ctgyCount, itemcnt); await context.DisposeAsync(); }
public async Task <IActionResult> Put([FromODataUri] int key, [FromBody] FinanceAccountCategory update) { if (!ModelState.IsValid) { HIHAPIUtility.HandleModalStateError(ModelState); } if (key != update.ID) { throw new BadRequestException("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.FinAccountCategories.Any(p => p.ID == key)) { return(NotFound()); } else { throw new DBOperationException(exp.Message); } } return(Updated(update)); }