Example #1
0
        public async Task <IActionResult> PutTerminus(int?id, Terminus update)
        {
            Terminus terminus;

            try
            {
                terminus = await _context.terminus.FindAsync(id);

                _context.Entry(terminus).State = EntityState.Detached;
            }
            catch (PostgresException e)
            {
                throw new NpgsqlException("Błąd serwera SQL - " + e.MessageText + " (kod " + e.SqlState + ")");
            }

            if (terminus != null)
            {
                foreach (PropertyInfo pi in typeof(Terminus).GetProperties())
                {
                    if ((pi.GetValue(update) != pi.GetValue(terminus)) && (pi.GetValue(update) != null))
                    {
                        _context.Entry(update).Property(pi.Name).IsModified = true;
                    }
                    else if (pi.Name.Equals("id"))
                    {
                        update.id = id;
                    }
                }
            }
            else
            {
                return(NotFound("Nie znaleziono"));
            }

            try
            {
                await _context.SaveChangesAsync();

                _context.Entry(update).State = EntityState.Detached;
                terminus = await _context.terminus.FindAsync(id);
            }
            catch (DbUpdateConcurrencyException e)
            {
                throw new DbUpdateConcurrencyException("Błąd podczas aktualizacji bazy danych - " + e.Message);
            }

            return(Ok(terminus));
        }
        public async Task <IActionResult> PutRelation(string id, Relation update)
        {
            Relation relation;

            try
            {
                relation = await _context.relations.FindAsync(id);

                _context.Entry(relation).State = EntityState.Detached;
            }
            catch (PostgresException e)
            {
                throw new NpgsqlException("Błąd serwera SQL - " + e.MessageText + " (kod " + e.SqlState + ")");
            }

            if (relation != null)
            {
                update.id = id;
                foreach (PropertyInfo pi in typeof(Relation).GetProperties())
                {
                    if ((pi.GetValue(update) != pi.GetValue(relation)) &&
                        (pi.GetValue(update) != null) &&
                        (!pi.Name.Equals("id")))
                    {
                        _context.Entry(update).Property(pi.Name).IsModified = true;
                    }
                }
            }
            else
            {
                return(NotFound("Nie znaleziono"));
            }

            try
            {
                await _context.SaveChangesAsync();

                _context.Entry(update).State = EntityState.Detached;
                relation = await _context.relations.FindAsync(id);
            }
            catch (DbUpdateConcurrencyException e)
            {
                throw new DbUpdateConcurrencyException("Błąd podczas aktualizacji bazy danych - " + e.Message);
            }

            return(Ok(relation));
        }