Example #1
0
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(weather).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!weatherExists(weather.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Example #2
0
        public async Task <BlogPostViewModel> Create(BlogPost post)
        {
            post.CreatedAt = DateTime.UtcNow;
            post.UpdatedAt = post.CreatedAt;
            _context.BlogPosts.Add(post);

            if (post.TagList == null || post.TagList.Count == 0)
            {
                goto noTags;
            }

            post.BlogPostTags = new List <BlogPostTags>();

            var dbTags = _context.Tags.Select(x => x.TagId);

            foreach (var tag in post.TagList)
            {
                if (!dbTags.Contains(tag))
                {
                    _context.Tags.Add(new Tags()
                    {
                        TagId = tag
                    });
                }

                BlogPostTags postTag = new BlogPostTags()
                {
                    TagId = tag,
                    Slug  = post.Slug
                };

                post.BlogPostTags.Add(postTag);
            }
            noTags : await _context.SaveChangesAsync();

            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <BlogPost, BlogPostViewModel>();
            });

            IMapper mapper = config.CreateMapper();

            var postVM = mapper.Map <BlogPostViewModel>(post);

            return(postVM);
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.weather.Add(weather);
            await _context.SaveChangesAsync();

            return(RedirectToRoute("weathers"));
        }
Example #4
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            weather = await _context.weather.FindAsync(id);

            if (weather != null)
            {
                _context.weather.Remove(weather);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }