Beispiel #1
0
        public IActionResult UpdateServis([FromBody] ServisApiModel model, int idServis)
        {
            if (model != null && ModelState.IsValid)
            {
                var servis = context.Servis.Find(idServis);
                if (servis == null)
                {
                    logger.LogError("Servis s ID=" + idServis + " ne postoji");
                    return(BadRequest("Servis s ID=" + idServis + " ne postoji"));
                }

                if (IBANExists(model.Racun) && !servis.ZiroRacun.Equals(model.Racun))
                {
                    return(BadRequest("IBAN postoji"));
                }



                servis.Ime       = model.Ime;
                servis.Opis      = model.Opis;
                servis.ZiroRacun = model.Racun;

                servis.TipServisa = new List <TipServisa>();
                var tip = new TipServisa
                {
                    Tip = model.TipServisa
                };
                servis.TipServisa.Add(tip);

                try
                {
                    context.Update(servis);
                    context.SaveChanges();
                    logger.LogInformation("Servis " + servis.Ime + " uspješno izmjenjen");
                    return(new ObjectResult(model));
                }
                catch (Exception ex)
                {
                    logger.LogError(ex.Message);
                    return(BadRequest(ex.Message));
                }
            }
            else
            {
                logger.LogError("Servis nije spremljen.");
                return(BadRequest("Servis nije spremljen."));
            }
        }
Beispiel #2
0
        public IActionResult CreateServis([FromBody] ServisApiModel model)
        {
            if (model != null)
            {
                if (IBANExists(model.Racun))
                {
                    return(BadRequest("IBAN postoji"));
                }

                Servis servis = new Servis
                {
                    Ime        = model.Ime,
                    Opis       = model.Opis,
                    ZiroRacun  = model.Racun,
                    TipServisa = new List <TipServisa>()
                };

                servis.TipServisa.Add(new TipServisa
                {
                    Tip = model.TipServisa
                });

                try
                {
                    servis.Serviser = new List <Serviser>();
                    context.Add(servis);
                    context.SaveChanges();

                    logger.LogInformation("Servis " + model.Ime + " uspješno dodan.");
                    return(new ObjectResult(servis));
                }
                catch (Exception ex)
                {
                    logger.LogError(ex.Message);
                    return(BadRequest(ex.Message));
                }
            }
            else
            {
                logger.LogError("Servis nije spremljen.");
                return(BadRequest("Servis nije spremljen."));
            }
        }