public IHttpActionResult Update(MonsterUpdateRequest monster) { if (monster == null || !ModelState.IsValid) { return(ResponseMessage(CreateCustomResponseMessage(HttpStatusCode.BadRequest))); } var result = _monsterService.Update(monster); return(ResponseMessage(CreateCustomResponseMessage(result))); }
public ServiceResponse <MonsterResponse> Update(MonsterUpdateRequest monster) { using (MonstersContext db = new MonstersContext()) { try { var monsterToUpdate = db.Monsters.FirstOrDefault(x => x.Id == monster.Id); if (monster.HP.HasValue) { monsterToUpdate.HP = monster.HP.Value; } if (monster.Exp.HasValue) { monsterToUpdate.Exp = monster.Exp.Value; } if (!string.IsNullOrEmpty(monster.Name)) { monsterToUpdate.Name = monster.Name; } if (monster.MovementSpeed.HasValue) { monsterToUpdate.MovementSpeed = monster.MovementSpeed.Value; } if (monster.SeeingInvisible != null) { monsterToUpdate.SeeingInvisible = monster.SeeingInvisible.Value; } if (!string.IsNullOrEmpty(monster.ImageLink)) { monsterToUpdate.ImageLink = monster.ImageLink; } db.SaveChanges(); return(new ServiceResponse <MonsterResponse>(new MonsterResponse(monsterToUpdate), HttpStatusCode.OK, "Monster updated succesfully!")); } catch (Exception ex) { return(new ServiceResponse <MonsterResponse>(null, HttpStatusCode.NotFound, "There isn't existing monster with given id")); } } }