public async Task <IActionResult> Register(Ong ong) { try { Random random = new Random(); ong.Id = random.Next(0, int.MaxValue); _context.Ong.Add(ong); await _context.SaveChangesAsync(); return(Ok(ong)); } catch (Exception ex) { return(BadRequest(ex)); } }
public async Task AddOrUpdate(HeroAddOrUpdateViewModel model) { var hero = await dbContext.Heroes.FindAsync(model.Id); if (hero != null) { mapper.Map(model, hero); await dbContext.SaveChangesAsync(); } else { var newHero = mapper.Map <Hero>(model); dbContext.Add(newHero); await dbContext.SaveChangesAsync(); } }
public async Task AddOrUpdate(PostAddOrUpdateViewModel model) { model.Body = model.Body.Replace("background-color: rgb(255, 255, 255);", ""); var post = await dbContext.Posts.FindAsync(model.Id); if (post != null) { mapper.Map(model, post); await dbContext.SaveChangesAsync(); } else { var newPost = mapper.Map <Post>(model); dbContext.Add(newPost); await dbContext.SaveChangesAsync(); } }
public async Task <IActionResult> Register(Incident incident) { try { int idOng = _helper.isAuthenticated(); if (idOng == 0) { return(Unauthorized()); } incident.IdOng = idOng; _context.Incident.Add(incident); await _context.SaveChangesAsync(); return(Ok(new { incident.Id })); } catch (Exception ex) { return(BadRequest(ex)); } }
// POST: api/Heroes public async Task <IHttpActionResult> Post([FromBody] HeroCreate hero) { if (hero == null || string.IsNullOrEmpty(hero.HeroName) || string.IsNullOrWhiteSpace(hero.HeroName)) { return(BadRequest(string.Format(GeneralMessages.InvalidData, ModelNames.Hero))); } using (HeroDbContext context = new HeroDbContext()) { context.Heroes.Add(new Hero(hero)); await context.SaveChangesAsync(); return(Ok(new CMessage(string.Format(GeneralMessages.CreateSuccess, ModelNames.Hero)))); } }
// DELETE: api/Heroes/5 public async Task <IHttpActionResult> Delete(int id) { using (HeroDbContext context = new HeroDbContext()) { Hero findHero = await context.Heroes.FirstOrDefaultAsync(h => h.HeroId == id); if (findHero == null) { return(BadRequest(string.Format(GeneralMessages.NotFound, ModelNames.Hero, _heroMessages.GetString("HeroId"), id))); } context.Heroes.Remove(findHero); await context.SaveChangesAsync(); return(Ok(new CMessage(string.Format(GeneralMessages.DeleteSuccess, ModelNames.Hero, _heroMessages.GetString("HeroId"), id)))); } }
public async Task <ActionResult> InsertComment([FromBody] PostComment comment) { bool status = false; if (ModelState.IsValid) { try { using (HeroDbContext db = new HeroDbContext()) { string IP = Request.HttpContext.Connection.RemoteIpAddress.ToString(); DateTime date = DateTime.Now; int heroCount = await db.Heroes.Where(h => h.HeroID == comment.HeroID).CountAsync(); if (heroCount > 0) { Comments cm = new Comments() { HeroID = comment.HeroID, Name = comment.Name.Trim(), Email = comment.Email.Trim(), Message = comment.Message.Trim(), IPAddress = IP, CommentDate = date }; db.Comments.Add(cm); int row = await db.SaveChangesAsync(); if (row > 0) { status = true; } } } } catch (Exception) { return(BadRequest()); } } return(Ok(status)); }
// PUT: api/Heroes/5 public async Task <IHttpActionResult> Put(int id, [FromBody] HeroCreate hero) { if (hero == null || string.IsNullOrEmpty(hero.HeroName) || string.IsNullOrWhiteSpace(hero.HeroName)) { return(BadRequest(string.Format(GeneralMessages.InvalidData, ModelNames.Hero))); } using (HeroDbContext context = new HeroDbContext()) { Hero findHero = await context.Heroes.FirstOrDefaultAsync(h => h.HeroId == id); if (findHero == null) { return(BadRequest(string.Format(GeneralMessages.NotFound, ModelNames.Hero, _heroMessages.GetString("HeroId"), id))); } findHero.HeroName = hero.HeroName; await context.SaveChangesAsync(); return(Ok(new CMessage(string.Format(GeneralMessages.UpdateSuccess, ModelNames.Hero, _heroMessages.GetString("HeroId"), id)))); } }