public async Task <ActionResult> OstaviRejting([FromBody] RejtingModel model) { try { if (model.Rejting < 1 || model.Rejting > 5) { ModelState.AddModelError(nameof(RejtingModel.Rejting), "Rejting može imati vrijednost od 1 do 5."); } if (!ModelState.IsValid) { return(BadRequest(model)); } return(Ok(await _kursInstancaDataService.OstaviRejting(model, UserResolver.GetKlijentId(HttpContext.User)))); } catch (Exception ex) { return(BadRequest(new ApiException(ex.Message, System.Net.HttpStatusCode.BadRequest))); } }
public async Task <int?> OstaviRejting(RejtingModel model, int klijentId) { try { var klijentInstanca = _context.KlijentKursInstanca .Where(k => k.KlijentId == klijentId && k.KursInstancaId == model.InstancaId) .FirstOrDefault(); if (klijentInstanca == null) { throw new Exception("Ne možete ostaviti rejting na ovaj kurs."); } klijentInstanca.Rejting = model.Rejting; await _context.SaveChangesAsync(); return(model.Rejting); } catch (Exception ex) { throw new Exception(ex.Message); } }