public async Task <IActionResult> Post([FromBody] FinanceDocumentType 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.Createdby = usrName; ctgy.CreatedAt = DateTime.Now; _context.FinDocumentTypes.Add(ctgy); await _context.SaveChangesAsync(); return(Created(ctgy)); }
public async Task <IActionResult> Put([FromODataUri] int key, [FromBody] FinanceDocumentType 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)) { throw new BadRequestException("Inputted Object IsValid failed"); } update.UpdatedAt = DateTime.Now; update.Updatedby = usrName; _context.Entry(update).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException exp) { if (!_context.FinDocumentTypes.Any(p => p.ID == key)) { throw new NotFoundException("Object ID not found"); } else { throw new DBOperationException(exp.Message); } } return(Updated(update)); }
public async Task TestCase1_Home1() { var ctgyCount = DataSetupUtility.FinanceDocumentTypes.Count(); var context = this.fixture.GetCurrentDataContext(); FinanceDocumentTypesController control = new FinanceDocumentTypesController(context); var user = DataSetupUtility.GetClaimForUser(DataSetupUtility.UserA); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = user } }; // 1. Read all categories var items = control.Get(); var itemcnt = items.Count(); Assert.Equal(ctgyCount, itemcnt); // 2. Insert new category var ctgy = new FinanceDocumentType() { HomeID = DataSetupUtility.Home1ID, Name = "Test 1", Comment = "Test 1" }; var rst1 = await control.Post(ctgy); Assert.NotNull(rst1); var rst2 = Assert.IsType <CreatedODataResult <FinanceDocumentType> >(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 it ctgy.Name = "Test 2"; rst1 = await control.Put(firstctg, ctgy); var rst3 = Assert.IsType <UpdatedODataResult <FinanceDocumentType> >(rst1); Assert.Equal(ctgy.Name, rst3.Entity.Name); // 5. Delete it var rst4 = await control.Delete((short)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(); }