Ejemplo n.º 1
0
        public static void UpdatePostSetting(BlogUserSetting setting)
        {
            string rootFolder = Path.Combine(Startup.BlogFolder, setting.DeployFolder);

            if (!Directory.Exists(rootFolder))
            {
                Directory.CreateDirectory(rootFolder);
            }

            String fileName = Path.Combine(rootFolder, SettingFile);
            var    sjon     = new BlogSettingJson();

            sjon.title  = setting.Name;
            sjon.footer = setting.Comment;

            try
            {
                var jsonString = JsonSerializer.Serialize(sjon);
                File.WriteAllText(fileName, jsonString);
            }
            catch (Exception exp)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(exp.Message);
#endif
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Put([FromODataUri] string owner, [FromBody] BlogUserSetting update)
        {
            if (!ModelState.IsValid)
            {
                HIHAPIUtility.HandleModalStateError(ModelState);
            }

            // User
            string usrName;

            try
            {
                usrName = HIHAPIUtility.GetUserID(this);
                if (String.IsNullOrEmpty(usrName))
                {
                    throw new UnauthorizedAccessException();
                }
                if (String.CompareOrdinal(update.Owner, usrName) != 0)
                {
                    throw new UnauthorizedAccessException();
                }
            }
            catch
            {
                throw new UnauthorizedAccessException();
            }

            //// Check setting
            //var setting = _context.BlogUserSettings.SingleOrDefault(p => p.Owner == usrName);
            //if (setting == null)
            //{
            //    throw new BadRequestException("User has no setting ");
            //}

            _context.Entry(update).State = EntityState.Modified;
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_context.BlogUserSettings.Any(p => p.Owner == update.Owner))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(update));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Post([FromBody] BlogUserSetting newsetting)
        {
            // Not Yet possible
            return(Forbid());
            //if (!ModelState.IsValid)
            //{
            //    HIHAPIUtility.HandleModalStateError(ModelState);
            //}

            //// User
            //string usrName;
            //try
            //{
            //    usrName = HIHAPIUtility.GetUserID(this);
            //    if (String.IsNullOrEmpty(usrName))
            //    {
            //        throw new UnauthorizedAccessException();
            //    }
            //    if (String.CompareOrdinal(newsetting.Owner, usrName) != 0)
            //    {
            //        throw new UnauthorizedAccessException();
            //    }
            //}
            //catch
            //{
            //    throw new UnauthorizedAccessException();
            //}

            //// Check setting
            //var setting = _context.BlogUserSettings.SingleOrDefault(p => p.Owner == usrName);
            //if (setting == null)
            //{
            //    throw new BadRequestException(" Setting already Exists ");
            //}

            //_context.BlogUserSettings.Add(newsetting);
            //await _context.SaveChangesAsync();

            //return Created(newsetting);
        }
        public async Task TestCase1(string user)
        {
            var context = this.fixture.GetCurrentDataContext();

            fixture.InitBlogTestData(context);

            var control   = new BlogUserSettingsController(context);
            var userclaim = DataSetupUtility.GetClaimForUser(user);
            var httpctx   = UnitTestUtility.GetDefaultHttpContext(provider, userclaim);

            control.ControllerContext = new ControllerContext()
            {
                HttpContext = httpctx
            };

            var existedamt = (from coll in context.BlogUserSettings where coll.Owner == user select coll).ToList().Count();

            // Step 1. Read all
            var rsts    = control.Get();
            var rstscnt = await rsts.CountAsync();

            Assert.Equal(existedamt, rstscnt);

            // Step 2. Create one new collection
            var nset = new BlogUserSetting()
            {
                Owner        = user,
                Name         = "Test Setting",
                Comment      = "Test Comment",
                DeployFolder = "Test"
            };
            var rst = await control.Post(nset);

            Assert.NotNull(rst);
            Assert.IsType <ForbidResult>(rst);

            // Step 3. Update one
            if (rstscnt > 0)
            {
                var existsett = control.Get(user);
                Assert.NotNull(existsett);
                var existsett2 = Assert.IsType <SingleResult <BlogUserSetting> >(existsett);
                Assert.NotNull(existsett2);
                var existsett2rst = existsett2.Queryable.FirstOrDefault();
                existsett2rst.Comment     = "Tobe Delteed";
                existsett2rst.Author      = "Author";
                existsett2rst.AuthorDesp  = "Author Desp";
                existsett2rst.AuthorImage = "Author Image";
                var rst3 = await control.Put(existsett2rst.Owner, existsett2rst);

                Assert.NotNull(rst3);
                var rst3a = Assert.IsType <OkObjectResult>(rst3);
                var rst3b = rst3a.Value as BlogUserSetting;
                Assert.Equal(existsett2rst.Comment, rst3b.Comment);
                Assert.Equal(existsett2rst.Author, rst3b.Author);
                Assert.Equal(existsett2rst.AuthorDesp, rst3b.AuthorDesp);
                Assert.Equal(existsett2rst.AuthorImage, rst3b.AuthorImage);
            }

            await context.DisposeAsync();
        }