public async Task <ActionResult <User> > PostUser(User user) { _context.Users.Add(user); await _context.SaveChangesAsync(); return(Ok(user)); }
public async Task <IActionResult> AssignResponsibleToItem(string id, long itemId) { var appUser = await _userManager.FindByIdAsync(id); if (appUser == null) { return(NotFound("user not found")); } var newsItem = await _context.NewsItems.FindAsync(itemId); if (newsItem == null) { return(NotFound("news item not found")); } newsItem.Responsible = appUser; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!NewsItemExists(itemId)) { return(NotFound("todo item not found")); } } return(NoContent()); }
public async Task <NewsPost> Post(NewsPost newNewsPost) { _context.NewsPost.Add(newNewsPost); await _context.SaveChangesAsync(); // Lagrer endringen i Databasen return(newNewsPost); // Kan returnere hva som helst feks String/Int eller Status Code feks 201 Created }
public async Task <IActionResult> PutTag(int id, Tag tag) { if (id != tag.TagID) { return(BadRequest()); } _context.Entry(tag).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TagExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutContactForm(long id, ContactEntity contactForm) { if (id != contactForm.Id) { return(BadRequest()); } var contact = await _context.ContactItems.FindAsync(id); if (contact == null) { return(NotFound()); } contact.CompleteName = contactForm.CompleteName; contact.Mail = contactForm.Mail; contact.Phone = contactForm.Phone; contact.Message = contact.Message; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!ContactFormExists(id)) { return(NotFound()); } return(NoContent()); }
public async Task <IActionResult> Create(News news) { var getFirstName = _userManager.GetUserAsync(User).Result.FirstName; var getLastName = _userManager.GetUserAsync(User).Result.LastName; string whoCreated = getFirstName + " " + getLastName; var getCurTime = DateTime.Now; var nw = new News { NewsType = news.NewsType, Article = news.Article, Description = news.Description, HideNews = news.HideNews, Img = news.Img, DateCreateNews = getCurTime, WhoCreated = whoCreated, IsNewsDay = news.IsNewsDay, IsNewsWeek = news.IsNewsWeek }; db.News.Add(nw); await db.SaveChangesAsync(); return(RedirectToAction("News")); }
public async Task <IActionResult> PutNews(int id, News news) { if (id != news.Id) { return(BadRequest()); } _context.Entry(news).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!NewsExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Edit(int id, [Bind("ID,PostTitle,PostContent,PostTeaser,AvatarImage,ViewCount,CateID")] Post post) { if (id != post.ID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(post); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostExists(post.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CateID"] = new SelectList(_context.Categories, "ID", "CategoryName", post.CateID); return(View(post)); }
public async Task <IActionResult> PutComment(int id, Comment Comment) { if (id != Comment.CommentID) { return(BadRequest()); } _context.Entry(Comment).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CommentExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Comment> > PostComment(Comment comment) { _context.Comments.Add(comment); await _context.SaveChangesAsync(); return(Ok(comment)); }
public async Task <IActionResult> PutCategory(int id, CategoryDTO category) { if (id != category.Id) { return(BadRequest("The category you pointed doesn't exist!")); } var news = _context.News; //проверка сущ-ия новостей foreach (var i in category.NewsId) { if (!news.Where(p => p.Id == i).Any()) { return(BadRequest("The News you pointed doesn't exist!")); } } var OldCategory = _context.Categories.Include(p => p.News).Where(p => p.Id == category.Id).FirstOrDefault(); OldCategory.News.Clear(); foreach (var i in category.NewsId) { OldCategory.News.Add(news.Find(i)); } OldCategory.Name = category.Name; OldCategory.Desc = category.Desc; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> PutArticle(int id, Article article) { if (id != article.ArticleID) { return(BadRequest()); } _context.Entry(article).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ArticleExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Like> > PostLike(Like like) { _context.Likes.Add(like); await _context.SaveChangesAsync(); return(Ok(like)); }
public async Task <IActionResult> Create(Guid id, [Bind("Id,Title,Summary,PostTime,Photo")] NewsViewModel new_news) { if (ModelState.IsValid) { News news = new News { Id = Guid.NewGuid(), Title = new_news.Title, Summary = new_news.Summary, PostTime = DateTime.Now }; if (new_news.Photo != null) { byte[] imageData = null; // считываем переданный файл в массив байтов using (var binaryReader = new BinaryReader(new_news.Photo.OpenReadStream())) { imageData = binaryReader.ReadBytes((int)new_news.Photo.Length); } // установка массива байтов news.Photo = imageData; Console.WriteLine(imageData); } _context.Add(news); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(RedirectToAction("Index")); }
public async Task <TModel> Create(TModel entity) { await _dbContext.Set <TModel>().AddAsync(entity); await _dbContext.SaveChangesAsync(); return(entity); }
public async Task <int> AddNews(New news) { _context.News.Add(news); await _context.SaveChangesAsync(); return(news.Id); }
public async Task CreateAsync(ApplicationUser user) { if (user == null) { throw new ArgumentNullException(); } context.ApplicationUsers.Add(user); await context.SaveChangesAsync(); }
/// <summary> /// Tạo mới danh mục /// </summary> /// <param name="request"></param> /// <returns>Thông tin danh mục</returns> public async Task <Response <CategoryResponse> > CreateCategoryAsync(CreatCategoryRequest request) { if (string.IsNullOrEmpty(request.Name)) { _logger.LogError("Name category is null"); return(new Response <CategoryResponse>(Constant.STATUS_ERROR, new List <string> { "Name category is null" })); } var category = new Category { Id = Guid.NewGuid(), Name = request.Name }; if (request.ParentId.HasValue) { var cate = await _newsContext.Categories.FindAsync(request.ParentId); if (cate == null) { _logger.LogError("Not find parent category"); return(new Response <CategoryResponse>(Constant.STATUS_ERROR, new List <string> { "Not find parent category" })); } category.ParentId = request.ParentId; } else { category.ParentId = null; } await _newsContext.Categories.AddAsync(category); var result = await _newsContext.SaveChangesAsync(); var dataResponse = new CategoryResponse { Id = category.Id, Name = category.Name, ParentId = category.ParentId }; if (result > 0) { _logger.LogInformation("Create category success"); return(new Response <CategoryResponse>(Constant.STATUS_SUCESS, null, dataResponse, 1)); } _logger.LogError("Create category failed"); return(new Response <CategoryResponse>(Constant.STATUS_ERROR, new List <string> { "Error when save" })); }
public async Task <IActionResult> Create([Bind("Id,Name,Description")] Category category) { if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(category)); }
public async Task <IActionResult> Create([Bind("UserId,Email")] User user) { if (ModelState.IsValid) { _context.Add(user); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(user)); }
public async Task <IActionResult> Create([Bind("Id,Name,DisplayName,Priority")] Langs langs) { if (ModelState.IsValid) { _context.Add(langs); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(langs)); }
public async Task <IActionResult> Create([Bind("AuthorId,AuthorName,SurName,UserName,PassWord,Email")] Author author) { if (ModelState.IsValid) { _context.Add(author); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(author)); }
public async Task <IActionResult> Create([Bind("QuestionID,Email,QuestionText")] Question question) { if (ModelState.IsValid) { _context.Add(question); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(question)); }
public async Task SaveNews(News news) { try { _context.Add(news); await _context.SaveChangesAsync(); } catch (Exception e) { Console.WriteLine(e.Message); } }
public async Task <IActionResult> Create(Teammember teammember) { if (ModelState.IsValid) { uploadphote(teammember); _context.Add(teammember); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(teammember)); }
public async Task <ActionResult <int> > Add([FromBody] NewsDto newsDto) { var news = _mapper.Map <News>(newsDto); _context.Add(news); await _context.SaveChangesAsync(); _eventBus.Publish(new NewsAddEvent(news)); return(CreatedAtAction(nameof(Get), new { id = news.Id }, news.Id)); }
public async Task <IActionResult> Create([Bind("Id,Value")] Topic topic) { if (ModelState.IsValid) { topic.Id = Guid.NewGuid(); _context.Add(topic); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(topic)); }
public async Task <IActionResult> Create([Bind("MediaId,Path,NewsId")] Media media) { if (ModelState.IsValid) { _context.Add(media); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["NewsId"] = new SelectList(_context.News, "NewsId", "LitleTitle", media.NewsId); return(View(media)); }
public async Task <IActionResult> PutNews(int id, NewsDTO news) { if (id != news.Id) { return(BadRequest("There is no news with ID You pointed")); } var categories = _context.Categories; //проверка сущ-ия новостей foreach (var i in news.CategoriesId) { if (!categories.Where(p => p.Id == i).Any()) { return(BadRequest("The Category you pointed doesn't exist!")); } } var comments = _context.Comments; //проверка сущ-ия новостей foreach (var i in news.CommentsId) { if (!comments.Where(p => p.Id == i).Any()) { return(BadRequest("The Comment you pointed doesn't exist!")); } } var OldNews = _context.News.Include(p => p.Categories).Include(p => p.Comments).Where(p => p.Id == news.Id).FirstOrDefault(); OldNews.Categories.Clear(); OldNews.Comments.Clear(); foreach (var i in news.CategoriesId) { OldNews.Categories.Add(categories.Find(i)); } foreach (var i in news.CommentsId) { OldNews.Comments.Add(comments.Find(i)); } OldNews.Img = news.Img; OldNews.Name = news.Name; OldNews.ShortDesc = news.ShortDesc; OldNews.Text = news.Text; OldNews.TimePublication = news.TimePublication; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <Article> Add(Article newsArticle) { if (await _context.Authors.FindAsync(newsArticle.Author.Id) == null) { _context.Add(newsArticle.Author); } _context.Add(newsArticle); await _context.SaveChangesAsync(); return(await GetArticleAsync(newsArticle.Id)); }