Beispiel #1
0
        public void TestModel(int hid)
        {
            var obj = new LearnObject();

            Assert.False(obj.IsValid(null));

            obj.HomeID     = hid;
            obj.CategoryID = 12;
            obj.Name       = "test";
            obj.Content    = "test";
            Assert.True(obj.IsValid(null));

            obj.HomeID = 0;
            Assert.False(obj.IsValid(null));

            obj.HomeID     = hid;
            obj.CategoryID = 0;
            Assert.False(obj.IsValid(null));

            obj.CategoryID = 12;
            obj.Name       = "";
            Assert.False(obj.IsValid(null));

            obj.Name    = "test";
            obj.Content = "";
            Assert.False(obj.IsValid(null));
        }
        public async Task <IActionResult> Post([FromBody] LearnObject obj)
        {
            if (!ModelState.IsValid)
            {
                HIHAPIUtility.HandleModalStateError(ModelState);
            }

            // Check
            if (!obj.IsValid(this._context))
            {
                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 == obj.HomeID && p.User == usrName).Count();

            if (hms <= 0)
            {
                throw new UnauthorizedAccessException();
            }

            obj.Createdby = usrName;
            obj.CreatedAt = DateTime.Now;
            _context.LearnObjects.Add(obj);
            await _context.SaveChangesAsync();

            return(Created(obj));
        }
        public async Task <IActionResult> Put([FromODataUri] int key, [FromBody] LearnObject 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.Updatedby             = usrName;
            update.UpdatedAt             = DateTime.Now;
            _context.Entry(update).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException exp)
            {
                if (!_context.LearnObjects.Any(p => p.ID == key))
                {
                    throw new NotFoundException("Inputted Object Not Found");
                }
                else
                {
                    throw new DBOperationException(exp.Message);
                }
            }

            return(Updated(update));
        }