Exemplo n.º 1
0
        public async Task <IHttpActionResult> PostAuth(Auth auth, int loggedId, string sessionKey)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Constants.VerifyUserErrors error = AuthHandler.VerifyUserSession(sessionKey, loggedId, db);
            if (error == Constants.VerifyUserErrors.OK)
            {
                AuthHandler.PostNewAuth(auth, db);
                try
                {
                    await db.SaveChangesAsync();

                    User foundUser = db.Users.FirstOrDefault(u => u.ID == auth.UserID);
                    if (foundUser != null)
                    {
                        User loggedUser = db.Users.FirstOrDefault(u => u.ID == loggedId);
                        if (loggedUser != null)
                        {
                            LogHandler.CreateLogEntry(db, loggedId, $"The user {loggedUser.Name} (ID: {loggedId}) has created the login information for {foundUser.Name} (ID: {foundUser.ID})", (int)LogHandler.RequestTypes.POST);
                        }
                    }
                }
                catch (DbUpdateException)
                {
                    if (AuthExists(auth.UserID))
                    {
                        return(Conflict());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(CreatedAtRoute("DefaultApi", new { id = auth.UserID }, auth));
            }
            return(StatusCode(CommonMethods.StatusCodeReturn(error)));
        }