public IActionResult UpdateCompanyPrice(CompanyPriceModel model) { var Result = new ResponseModel(); try { if (!ModelState.IsValid) { Result.ErrorMessages.AddRange(ModelState.GetModelStateErrors()); return(BadRequest(Result)); } var data = _CompanyMarketService.Set().FirstOrDefault(x => x.MarketId == model.MarketId && x.CompanyId == model.CompanyId); if (data == null) { Result.StatusCode = HttpStatusCode.NotFound; return(NotFound(Result)); } data.Price = model.Price; _CompanyMarketService.Save(data); Result.StatusCode = HttpStatusCode.OK; return(Ok(Result)); } catch (Exception e) { Result.StatusCode = HttpStatusCode.BadRequest; Result.ErrorMessages.Add(e.Message); _logger.LogError($"Error at {DateTime.Now}. Message : {e.Message}, StackTrace : {e.StackTrace}"); return(BadRequest(e.Message)); } }
public async Task <object> AddOrUpdateCompanyPrice(CompanyPriceModel data) { try { var foundCompany = await _marketContext.MarketPrices.FirstOrDefaultAsync(x => x.Company.Id == data.CompanyId && x.Market.Id == data.MarketId); if (foundCompany != null) { foundCompany.Price = data.Price; foundCompany.DateUpdated = DateTime.Now; _marketContext.Attach(foundCompany); _marketContext.Entry(foundCompany).Property(p => p.Price).IsModified = true; _marketContext.Entry(foundCompany).Property(p => p.DateUpdated).IsModified = true; await _marketContext.SaveChangesAsync(); } else { await _marketContext.MarketPrices.AddAsync(new MarketPrice { CompanyId = data.CompanyId, MarketId = data.MarketId, Price = data.Price, DateUpdated = DateTime.Now }); await _marketContext.SaveChangesAsync(); } return(new { Status = true }); } catch (Exception ex) { return(new { Status = false, Message = $"error message - {ex.Message}" }); } }
public async Task <IActionResult> AddOrUpdateCompanyPrice([FromForm] CompanyPriceModel data) { return(Ok(await _companyService.AddOrUpdateCompanyPrice(data))); }