Пример #1
0
        public T Insert(T obj)
        {
            _context.Set <T>().Add(obj);
            _context.SaveChanges();

            return(obj);
        }
Пример #2
0
        // POST api/reservations
        public HttpResponseMessage Post([FromBody] Reservation reservation)
        {
            HttpResponseMessage response = HomeController.ValidateAuthorizationHeader(Request, out string username);

            if (reservation != null && reservation.rsvdateTime > DateTimeOffset.Now.ToUnixTimeMilliseconds() && courts.Contains(reservation.courtId))
            {
                var         user           = db.User.FirstOrDefault(u => u.username == username);
                Reservation newReservation = new Reservation(reservation.courtId, reservation.rsvdateTime);
                newReservation.username = username;
                if (horas.Contains(newReservation.rsvtime))
                {
                    string day = DateTimeOffset.FromUnixTimeMilliseconds(reservation.rsvdateTime).LocalDateTime.ToString("yyyy/MM/dd");
                    var    reservationsByDay = db.Reservation.Where(r => r.rsvday == day).ToList();
                    if (reservationsByDay.Any(r => r.username == username && r.rsvday == newReservation.rsvday && r.rsvtime == newReservation.rsvtime))
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Ya tienes otra reserva para el mismo día y hora indicada."));
                    }
                    if (reservationsByDay.Any(r => r.courtId == newReservation.courtId && r.rsvday == newReservation.rsvday && r.rsvtime == newReservation.rsvtime))
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest, "Esta pista está reservada para la fecha y hora indicada."));
                    }
                    var userReservations = db.Reservation.Where(r => r.username == username).ToList();
                    if (userReservations.Count > 3)
                    {
                        return(Request.CreateResponse(HttpStatusCode.Conflict, "Solo puedes realizar un máximo de 4 reservas."));
                    }
                    try
                    {
                        db.Reservation.Add(newReservation);
                        db.SaveChanges();
                        response.StatusCode = HttpStatusCode.OK;
                        return(response);
                    }
                    catch (Exception e)
                    {
                        return(Request.CreateResponse(HttpStatusCode.InternalServerError, string.Format("{0} - {1}", ERROR, e.Message)));
                    }
                }
            }
            return(Request.CreateResponse(HttpStatusCode.BadRequest));
        }
 public bool Commit()
 {
     try{
         if (transaction != null)
         {
             _context.SaveChanges();
         }
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Пример #4
0
 // POST: api/users
 public HttpResponseMessage Post([FromBody] User user)
 {
     if (user != null && !string.IsNullOrEmpty(user.username) && !string.IsNullOrEmpty(user.password) && !string.IsNullOrWhiteSpace(user.email))
     {
         var usuarioDb = db.User.FirstOrDefault(u => u.username == user.username);
         if (usuarioDb == null)
         {
             try
             {
                 user.salt     = HomeController.CreateSalt();
                 user.password = HomeController.HashPassword(user.password, user.salt);
                 db.User.Add(user);
                 db.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.Created));
             }
             catch (Exception e)
             {
                 return(Request.CreateResponse(HttpStatusCode.InternalServerError, string.Format("{0} - {1}", ERROR, e.Message)));
             }
         }
         return(Request.CreateResponse(HttpStatusCode.Conflict, "usuario duplicado"));
     }
     return(Request.CreateResponse(HttpStatusCode.BadRequest, "usuario no válido"));
 }
Пример #5
0
        public int Buscar(string nome)
        {
            Sequencial sequencial = (from q in _db.Sequenciais where q.Nome == nome select q).FirstOrDefault();

            if (sequencial == null)
            {
                return(0);
            }

            try
            {
                sequencial.Valor += 1;
                _db.SaveChanges();
            }
            catch
            {
                return(0);
            }

            return(sequencial.Valor);
        }
Пример #6
0
 /*  */
 public int SaveChanges() => _context.SaveChanges();
Пример #7
0
 public int SaveChanges()
 {
     return(Db.SaveChanges());
 }
Пример #8
0
 public bool Commit()
 {
     return(_context.SaveChanges() > 0);
 }