/// <summary> /// Create a new key for the already existing client /// </summary> /// <param name="id">Client Id</param> /// <param name="status">Client Key Status</param> /// <returns><![CDATA[ (ClientKeyVm key, bool IsSuccess, String Message) ]]></returns> public (ClientKeyVm key, bool IsSuccess, String Message) CreateKey(int id, ClientKeyStatus status) { try { var dbCheck = ClientDataAccess.Client.Find(f => f.Id == id); if (dbCheck == null) { return(null, false, ResourceManagerMessages.Error.CLIENT_NOT_FOUND); } // Generate a new Client Key var dbKey = (new ClientKeyVm() { Status = status }).ToEntityCreate(id); dbKey = ClientDataAccess.ClientKey.Create(dbKey); ClientDataAccess.Save(); var key = new ClientKeyVm(dbKey); return(key, true, ResourceManagerMessages.Success.CLAIM_KEY_CREATED); } catch (DbEntityValidationException ex) { #if (DEBUG) // for debugging entity framework foreach (var error in ex.EntityValidationErrors.SelectMany(valError => valError.ValidationErrors)) { Console.WriteLine(error.ErrorMessage); } #endif throw; } catch { throw; } }
/// <summary> /// Update the already existing key /// </summary> /// <param name="id">Client Key Id</param> /// <param name="status">Client Key Status</param> /// <returns><![CDATA[ (bool IsSuccess, String Message) ]]></returns> public (bool IsSuccess, String Message) UpdateKeyStatus(int id, ClientKeyStatus status) { try { // Check if the record exist in the database. var dbView = ClientDataAccess.ClientKey.Find(f => f.Id == id); if (dbView == null) { return(false, ResourceManagerMessages.Error.CLIENT_KEY_NOT_FOUND); } // Update the existing record from the database. dbView.Status = status; dbView.UpdateDate = DateTime.UtcNow; ClientDataAccess.ClientKey.Update(dbView); ClientDataAccess.Save(); return(true, ResourceManagerMessages.Success.CLAIM_KEY_UPDATED); } catch (DbEntityValidationException ex) { #if (DEBUG) // for debuging entity framework foreach (var error in ex.EntityValidationErrors.SelectMany(valError => valError.ValidationErrors)) { Console.WriteLine(error.ErrorMessage); } #endif throw; } catch { throw; } }
/*TODO : MAKE SURE THAT ALL THE TRANSACTION ON SET ARE LOGGED IN A TABLE*/ /// <summary> /// Create a new client record. /// </summary> /// <param name="ownerId">Owner Id</param> /// <param name="name">Client Name</param> /// <param name="status">Client Status</param> /// <returns><![CDATA[ (ClientVm Client, ClientKeyVm Key, bool IsSuccess, String Message) ]]></returns> public (ClientVm Client, ClientKeyVm Key, bool IsSuccess, String Message) CreateClient(int ownerId, string name, ClientKeyStatus status) { try { var dbCheck = ClientDataAccess.Client.Find(f => f.OwnerId == ownerId && f.Name.ToLower() == name.ToLower()); if (dbCheck != null) { return(null, null, false, ResourceManagerMessages.Error.CLIENT_ADD_ALREADY_EXISTS); } // Create the Client Resource var dbView = (new ClientVm() { Name = name }).ToEntityCreate(ownerId); dbView = ClientDataAccess.Client.Create(dbView); // Generate a new Client Key var dbKey = (new ClientKeyVm() { Status = status }).ToEntityCreate(dbView.Id); dbKey = ClientDataAccess.ClientKey.Create(dbKey); ClientDataAccess.Save(); var client = new ClientVm(dbView); var key = new ClientKeyVm(dbKey); return(client, key, true, ResourceManagerMessages.Success.CLAIM_CREATED); } catch (DbEntityValidationException ex) { #if (DEBUG) // for debuging entity framework foreach (var error in ex.EntityValidationErrors.SelectMany(valError => valError.ValidationErrors)) { Console.WriteLine(error.ErrorMessage); } #endif throw; } catch { throw; } }