예제 #1
0
        public async Task <int> SavePostAsync(PostViewModel model, bool delete = false)
        {
            var post = (Post)model;

            if (model.Id != 0)
            {
                var item = await _guavaDbContext
                           .Posts
                           .AsNoTracking()
                           .FirstOrDefaultAsync(p => p.Id == model.Id);

                if (item != null && !delete)
                {
                    _guavaDbContext.Entry(post).State = EntityState.Modified;
                }
                else if (item != null)
                {
                    _guavaDbContext.Entry(post).State = EntityState.Deleted;
                }
            }
            else
            {
                _guavaDbContext
                .Posts
                .Add(post);
            }
            return(await _guavaDbContext.SaveChangesAsync());
        }
예제 #2
0
        public async Task <int> SaveMetaData(BlogViewModel model)
        {
            var blog = (Blog)model;

            if (model.Logo?.Length > 0)
            {
                var fileName = $@"{_env.WebRootPath}\{_blogOptions.Value.ImagesFolder}\{model.Logo.FileName}";

                using (var stream = new FileStream(fileName, FileMode.Create))
                {
                    await model.Logo.CopyToAsync(stream);
                }
                blog.Logo = $@"{_blogOptions.Value.ImagesFolder}\{model.Logo.FileName}";;
            }

            if (model.Cover?.Length > 0)
            {
                var fileName = $@"{_env.WebRootPath}\{_blogOptions.Value.ImagesFolder}\{model.Cover.FileName}";
                using (var stream = new FileStream(fileName, FileMode.Create))
                {
                    await model.Cover.CopyToAsync(stream);
                }
                blog.Cover = $@"{_blogOptions.Value.ImagesFolder}\{model.Cover.FileName}";
            }

            if (model.Id != 0)
            {
                var item = await _guavaDbContext
                           .Blogs
                           .AsNoTracking()
                           .FirstOrDefaultAsync();

                if (item != null)
                {
                    _guavaDbContext.Entry(blog).State = EntityState.Modified;
                }
            }
            else
            {
                _guavaDbContext
                .Blogs
                .Add(blog);
            }



            return(await _guavaDbContext.SaveChangesAsync());
        }