Esempio n. 1
0
        public static GetEventsResponse GetEvents(int userId, DateTime start, DateTime end, int nb)
        {
            //TODO : See why when the requested Date contains hours, dosn't work !
            try
            {
                using (var bdd = new popopopoEntities())
                {
                    var results = bdd.Events.Where(e => e.creator_id == userId &&
                                                   e.start_date >= start &&
                                                   e.end_date <= end)
                                  .OrderBy(e => e.start_date)
                                  .Take(nb)
                                  .ToList();

                    return(new GetEventsResponse {
                        Message = "Done", Status = HttpStatusCode.OK, Events = results
                    });
                }
            }
            catch (Exception)
            {
                return(new GetEventsResponse {
                    Message = "SQLError", Status = HttpStatusCode.InternalServerError, Events = new List <Event>()
                });
            }
        }
Esempio n. 2
0
        public static CreateAccountResponse AddAccount(Account account)
        {
            try
            {
                using (Models.popopopoEntities bdd = new popopopoEntities())
                {
                    bool alreadyHasEmail    = bdd.Accounts.Where(a => a.email == account.email).Count() != 0;
                    bool alreadyHasUsername = bdd.Accounts.Where(a => a.username == account.username).Count() != 0;
                    bool alreadyHasId       = true;

                    do
                    {
                        account.ID = ToolsBox.GetNewId();

                        alreadyHasId = bdd.Accounts.Where(a => a.ID == account.ID).Count() != 0;
                    } while (alreadyHasId);

                    if (alreadyHasEmail)
                    {
                        return new CreateAccountResponse {
                                   Message = "EmailTaken", Status = HttpStatusCode.BadRequest
                        }
                    }
                    ;

                    if (alreadyHasUsername)
                    {
                        return new CreateAccountResponse {
                                   Message = "UsernameTaken", Status = HttpStatusCode.BadRequest
                        }
                    }
                    ;

                    bdd.Accounts.Add(account);
                    bdd.SaveChanges();
                }

                return(new CreateAccountResponse {
                    Message = "OK", Status = HttpStatusCode.OK, Id = account.ID
                });
            }
            catch (Exception)
            {
                return(new CreateAccountResponse {
                    Message = "SQLError", Status = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 3
0
        public static AccountResponse GetUserInfoByEmail(string email, string password)
        {
            try
            {
                using (Models.popopopoEntities bdd = new popopopoEntities())
                {
                    var receipt = bdd.Accounts.Where(a => a.email == email).ToList();

                    if (receipt.Count == 0)
                    {
                        return new AccountResponse {
                                   Message = "No user found whit this email", Status = HttpStatusCode.BadRequest, Account = null
                        }
                    }
                    ;

                    var account = receipt[0];
                    if (account == null)
                    {
                        return new AccountResponse {
                                   Message = "No user found whit this email", Status = HttpStatusCode.BadRequest, Account = null
                        }
                    }
                    ;

                    if (account.password != password)
                    {
                        return new AccountResponse {
                                   Message = "Password is not correct", Status = HttpStatusCode.BadRequest, Account = null
                        }
                    }
                    ;

                    return(new AccountResponse {
                        Message = "OK", Status = HttpStatusCode.OK, Account = account
                    });
                }
            }
            catch (Exception)
            {
                return(new AccountResponse {
                    Status = HttpStatusCode.InternalServerError, Account = null, Message = "Internal server Error"
                });
            }
        }
Esempio n. 4
0
        public static BasicDataResponse ExistsWithUsername(string username, string password)
        {
            try
            {
                using (Models.popopopoEntities bdd = new popopopoEntities())
                {
                    //TODO : Check if there is no other email (there we consider the add function worked well)
                    var receipt = bdd.Accounts.Where(a => a.username == username).ToList();

                    if (receipt.Count == 0)
                    {
                        return new BasicDataResponse {
                                   Message = "No user found whit this login", Status = HttpStatusCode.BadRequest
                        }
                    }
                    ;

                    var account = receipt[0];
                    if (account == null)
                    {
                        return new BasicDataResponse {
                                   Message = "No user found whit this login", Status = HttpStatusCode.BadRequest
                        }
                    }
                    ;

                    if (account.password != password)
                    {
                        return new BasicDataResponse {
                                   Message = "Password is not correct", Status = HttpStatusCode.BadRequest
                        }
                    }
                    ;
                }
                return(new BasicDataResponse {
                    Message = "OK", Status = HttpStatusCode.OK
                });
            }
            catch (InvalidOperationException)
            {
                return(new BasicDataResponse {
                    Message = "SQLError", Status = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 5
0
        public static BasicDataResponse Exists(ApiKey request)
        {
            try
            {
                using (var dbb = new popopopoEntities())
                {
                    var keys = dbb.ApiKeys.Where(ak => ak.Value == request.Value);

                    if (keys == null)
                    {
                        return new BasicDataResponse {
                                   Message = "Internal Error", Status = HttpStatusCode.InternalServerError
                        }
                    }
                    ;

                    if (keys.Count() == 0)
                    {
                        return new BasicDataResponse {
                                   Message = "Api key not registered", Status = HttpStatusCode.Forbidden
                        }
                    }
                    ;

                    //TODO : try to remove this condition !
                    if (keys.ToList()[0].Value != request.Value)
                    {
                        return new BasicDataResponse {
                                   Message = "Api key not registered", Status = HttpStatusCode.Forbidden
                        }
                    }
                    ;

                    return(new BasicDataResponse {
                        Message = "OK", Status = HttpStatusCode.OK
                    });
                }
            }
            catch (Exception)
            {
                return(new BasicDataResponse {
                    Message = "Can't check api authorization", Status = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 6
0
        public static AccountResponse GetUserInfoByUsername(string username)
        {
            try
            {
                using (Models.popopopoEntities bdd = new popopopoEntities())
                {
                    var receipt = bdd.Accounts.Where(a => a.username == username).ToList();

                    if (receipt.Count == 0)
                    {
                        return new AccountResponse {
                                   Message = "No user found whit this username", Status = HttpStatusCode.BadRequest, Account = null
                        }
                    }
                    ;

                    var account = receipt[0];
                    if (account == null)
                    {
                        return new AccountResponse {
                                   Message = "No user found whit this username", Status = HttpStatusCode.BadRequest, Account = null
                        }
                    }
                    ;

                    return(new AccountResponse {
                        Message = "OK", Status = HttpStatusCode.OK, Account = account
                    });
                }
            }
            catch (Exception)
            {
                return(new AccountResponse {
                    Status = HttpStatusCode.InternalServerError, Account = null, Message = "Internal server Error"
                });
            }
        }
Esempio n. 7
0
        public static BasicDataResponse AddEvent(Event e)
        {
            if (e == null)
            {
                return new BasicDataResponse()
                       {
                           Message = "request is null", Status = HttpStatusCode.BadRequest
                       }
            }
            ;

            try
            {
                using (var bdd = new popopopoEntities())
                {
                    bool alreadyHasId = false;
                    do
                    {
                        e.id         = ToolsBox.GetNewId();
                        alreadyHasId = bdd.Events.Where(ev => ev.id == e.id).Count() != 0;
                    } while (alreadyHasId);

                    bdd.Events.Add(e);
                    bdd.SaveChanges();

                    return(new BasicDataResponse {
                        Message = "Done", Status = HttpStatusCode.OK
                    });
                }
            }
            catch (Exception)
            {
                return(new BasicDataResponse {
                    Message = "SQLError", Status = HttpStatusCode.InternalServerError
                });
            }
        }
Esempio n. 8
0
        public static BasicDataResponse ExistsWithId(int id)
        {
            try
            {
                using (Models.popopopoEntities bdd = new popopopoEntities())
                {
                    var receipt = bdd.Accounts.Where(a => a.ID == id).ToList();

                    if (receipt.Count == 0)
                    {
                        return new BasicDataResponse {
                                   Message = "No user found whit this login", Status = HttpStatusCode.BadRequest
                        }
                    }
                    ;

                    var account = receipt[0];
                    if (account == null)
                    {
                        return new BasicDataResponse {
                                   Message = "No user found whit this login", Status = HttpStatusCode.BadRequest
                        }
                    }
                    ;
                }
                return(new BasicDataResponse {
                    Message = "OK", Status = HttpStatusCode.OK
                });
            }
            catch (InvalidOperationException)
            {
                return(new BasicDataResponse {
                    Message = "SQLError", Status = HttpStatusCode.InternalServerError
                });
            }
        }