public ActionResult Post(PlayerScrapy player) { ModelBaseReturn retorno = null; try { retorno = _playerService.SaveFromScrapy <PlayerScrapy>(player); if (retorno.Status == System.Net.HttpStatusCode.Created) { retorno.UrlCreated = Url.Action("Get", "Player", new { name = player.Name }); } return(ReturnBasedServiceLayer(retorno)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public ModelBaseReturn SaveFromScrapy(IList <PlayerScrapy> models) { ModelBaseReturn retornoErro = null; if (models != null && models.Any()) { var playerNames = models.Where(v => v is PlayerScrapy).Select(p => p.Name).ToArray(); var playersInDb = _repository.FindAllByNames(playerNames); foreach (var item in models) { var playerDb = playersInDb.FirstOrDefault(p => p.Name == item.Name); this.SaveFromScrapy(item, playerDb); } _repository.Save(); } return(retornoErro ?? CreateReturnCreated()); }
protected virtual ActionResult ReturnBasedServiceLayer(ModelBaseReturn returnModel) { switch (returnModel.Status) { case HttpStatusCode.Created: return(Created(returnModel.UrlCreated, returnModel.Model)); case HttpStatusCode.NoContent: return(NoContent()); case HttpStatusCode.OK: return(Ok(returnModel.Model)); case HttpStatusCode.NotFound: return(NotFound(returnModel.Model)); case HttpStatusCode.BadRequest: case HttpStatusCode.InternalServerError: return(BadRequest(new { Message = returnModel.Message, ErrorData = returnModel.Model })); } throw new ArgumentException("Status Code no supported in return", nameof(returnModel)); }