[Consumes("application/json")] // application/xml public IActionResult Post([FromBody] Four model) { try { if (model.Note == null || model.Note.Length < 2) { ModelState.AddModelError("Note", "노트는 2자 이상 입력해야 합니다."); } // 모델 유효성 검사 if (!ModelState.IsValid) { return(BadRequest(ModelState)); // 400 에러 출력 } var m = _repository.Add(model); if (DateTime.Now.Second % 2 == 0) //[!] 둘 중 원하는 방식 사용 { //return CreatedAtAction("GetById", new { id = m.Id }, m); // 201 return(CreatedAtRoute("GetById", new { id = m.Id }, m)); } else { var uri = Url.Link("GetById", new { id = m.Id }); return(Created(uri, m)); // 201 } } catch { return(BadRequest()); } }
public Four Update(Four model) { var sql = "Update Fours " + "Set " + " Note = @Note " + "Where Id = @Id "; db.Execute(sql, model); return(model); }
public Four Add(Four model) { string sql = @" Insert Into Fours (Note) Values (@Note); Select Cast(SCOPE_IDENTITY() As Int); "; var id = db.Query <int>(sql, model).Single(); model.Id = id; return(model); }
public IActionResult Post([FromBody] Four model) { try { // 모델 유효성 검사 if (!ModelState.IsValid) { return(BadRequest(ModelState)); // 400 에러 출력 } var m = _repository.Add(model); return(CreatedAtAction("Get", new { id = m.Id }, m)); // 201 } catch { return(BadRequest()); } }
[HttpPut("{id:int}")] // HttpPatch == 부분 업데이트 public IActionResult Put(int id, [FromBody] Four model) { if (model == null) { BadRequest(); } try { var oldModel = _repository.GetById(id); if (oldModel == null) { return(NotFound($"{id}번 데이터가 없습니다.")); } model.Id = id; // * _repository.Update(model); //return Ok(model); return(NoContent()); // 이미 던져준 정보에 모든 값 가지고 있기에 } catch (Exception) { } return(BadRequest("데이터가 업데이트되지 않았습니다.")); }